C++ code in C file

B

Bhan

I have written code inside a c file in C++ Style(NOTE: No objects are
created by myself.I am just using singleton objects in my c file.Also
used extern "c").I am able to compile,link and test.

But my question is that how I am able to get singleton instances in a C
file?

Is that writing C++ Code in C and viceversa doesn't matter as long as
you use extern for c type of calls?

Also.how does the compiler treat the code?
- Does it treat the code inside a c file as just some piece of code of
C or c++ language?(as translation unit and not bother about the file
name?)


If yes,Can I define classes in c files and compile successfully using a
C++ Compiler?


By the way I am using GNU Compiler for C++.
 
D

Dan Cernat

Bhan said:
I have written code inside a c file in C++ Style(NOTE: No objects are
created by myself.I am just using singleton objects in my c file.Also
used extern "c").I am able to compile,link and test.

But my question is that how I am able to get singleton instances in a C
file?

Is that writing C++ Code in C and viceversa doesn't matter as long as
you use extern for c type of calls?

Also.how does the compiler treat the code?
- Does it treat the code inside a c file as just some piece of code of
C or c++ language?(as translation unit and not bother about the file
name?)


If yes,Can I define classes in c files and compile successfully using a
C++ Compiler?


By the way I am using GNU Compiler for C++.


I cannot figure out what exactly you did and what you want to do. Show
us some code. Otherwise the answer is yes ... or no.

/dan
 
B

Bhan

Sorry for confusing...Dont bother abt my program
This is my requirement...

1.Is that writing C++ Code in C and viceversa doesn't matter as long as

you use extern for c type of calls?


2.Also.how does the compiler treat the code?
- Does it treat the code inside a c file as just some piece of code of
C or c++ language?(as translation unit and not bother about the file
name?).If yes,Can I define classes in c files and compile successfully
using a C++ Compiler?
 
D

Dan Cernat

Bhan said:
Sorry for confusing...Dont bother abt my program
This is my requirement...

1.Is that writing C++ Code in C and viceversa doesn't matter as long as

you use extern for c type of calls?


2.Also.how does the compiler treat the code?
- Does it treat the code inside a c file as just some piece of code of
C or c++ language?(as translation unit and not bother about the file
name?).If yes,Can I define classes in c files and compile successfully
using a C++ Compiler?

Writing C++ code in C? WTF is that? There was a joke going around: *a
good programmer can write FORTRAN in any language*. Is this what you
are trying to do?

Usually the compilers will look at the file extension. if it is .CPP it
will compile it as C++. If it is .C it will compile it as C. If you use
C++ features in a .C file, the compiler might choke.

/dan
 
M

Markus Moll

Hi
Sorry for confusing...Dont bother abt my program
This is my requirement...

1.Is that writing C++ Code in C and viceversa doesn't matter as long as

you use extern for c type of calls?

Short answer: You cannot write C++ programs in C and vice-versa (this would
mean C == C++ syntactically, wouldn't it?)

Long answer: As C++ is based upon C, a lot of C++ code actually also is
valid C code (and vice-versa). E.g. as long as you stick to global
functions (not using bool, inline (introduced in C99, wasn't it?), class,
templates or other C++ stuff) your code is likely to compile fine with a C
compiler.

The extern "C" declaration is only needed if you want to _link_ C code to
your _C++_ application.
2.Also.how does the compiler treat the code?
- Does it treat the code inside a c file as just some piece of code of
C or c++ language?(as translation unit and not bother about the file
name?).

A C++ compiler will treat your code as C++, a C compiler as C, a Java
compiler as Java, a Haskell compiler as Haskell... what else do you expect?

Your compiler frontend might choose to switch between compilers depending on
the file name (or extension). The manual should tell you details.
If yes,Can I define classes in c files and compile successfully
using a C++ Compiler?

No, you cannot add C++ code to a C file and expect it to compile; just one
of a dozen of simple reasons:

int throw(void)
{
}

is legal C code...


Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top