473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

C++ code in C file

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++.

Oct 5 '05 #1
4 1331

Bhan wrote:
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

Oct 5 '05 #2
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?

Oct 5 '05 #3

Bhan wrote:
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

Oct 5 '05 #4
Hi

Bhan wrote:
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
Oct 5 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc | last post by:
Hello I have a website where I can upload a file via a form. The uploaded file is stored in a MySQL database. When dloading te file I get a save as... dialog box, letting me save the file. But...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
19
by: Alan Silver | last post by:
Hello, I have been developing ASP.NET with a text editor so far (better way to learn initially), and have now been trying Visual Web Developer. I want to create a project for an existing site...
4
by: jason.awlt | last post by:
Greetings, I recently being bugged by the following error on my DB2. SQL0902C SQLSTATE = 58005 Reason Code = 14 This error comes out everytime i tried to insert some record to a table...
1
by: cnixuser | last post by:
Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text...
3
by: melbourno | last post by:
I have a Perl script that I run and the out come as showing below: Log chain 1: Reading: server 2\08120700.mls Reading: server 2\08120900.mls Reading: server 1\08121100.mls Log chain 2: ...
4
NeoPa
by: NeoPa | last post by:
Introduction: Macro Security Levels in MS Office applications are recommended to be set to High. This stops any VBA code associated with a project from running, unless it is signed (with a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.