473,799 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it a good thing that program mix C and C++?

Is it a good thing that program mix C and C++?

Mar 28 '07
45 2076
In article <46************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.netwrote :
>Is it a good thing that program mix C and C++?
>Mix, no. You can call C routines from C++, but not the reverse.
The C language does not define a mechanism for calling C++ functions,
but it is possible to do so in almost all (if not all) implementations .

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 28 '07 #11
>>>>"SM" == SM Ryan
>>>><wy*****@ta ngo-sierra-oscar-foxtrot-tango.fake.orgw rites:
SM"dolphin" <jd*******@gmai l.comwrote: # Is it a good thing
SMthat program mix C and C++?

SMIt is a good thing to get the program finished and running
SMcorrectly.

It is an even better thing to get the program finished, running
correctly, and in such a state that it is easy to alter or maintain.

That last point in the tricolon is a pretty clear argument against
mixing C and C++ in a logical unit; a library, for instance, may be in
C or C++, but it should never be in both simultaneously, because that
will make it considerably harder to maintain.

Charlton
--
Charlton Wilbur
cw*****@chromat ico.net
Mar 28 '07 #12

Ian Collins <ia******@hotma il.comwrote in message
news:56******** *****@mid.indiv idual.net...
Richard Heathfield wrote:
Ian Collins said:
>CBFalconer wrote:
dolphin wrote:

Is it a good thing that program mix C and C++?

Mix, no. You can call C routines from C++, but not the reverse.

No so, C++ provides a mechanism (the extern "C" linkage specifier) to
make C++ functions callable from C.
No, it doesn't. It makes C functions callable from C++.
All together now: oh yes it does!

It's commonly used where a C++ function has to be passed to a C library
as a callback.

extern "C" specified the linkage type of the function, not its language.
Uh, yeah. It prevents the "name-mangling" of functions compiled
under C++, allowing them to be called from C. It also explicitly
identifies that external functions compiled under C are not "name-mangled"
for proper linking with C++ object files. Once you understand this,
and how to do it, you can call back and forth between C and C++
object files pretty much at will. It is quite obvious that some people
here who are experts on the return value of main() don't understand
this.

Another additional rule is that your C++ functions must not take
or return any data types not found in C. This will quite often require
some special handling of C++ library functions not originally written
to be called from C, but generally doesn't require more than calling
them from a "wrapper" function that takes and returns the allowed
types.

As a response to the original question, it is a "good thing"
to not re-invent libraries just because they may have been
developed in C++ and your C program needs some functionality
in the C++ libraries, or you have a large collection of C libraries
you wrote and now want to use in a C++ program, and other
similar situations. As far as just using stuff from the C standard
libraries in C++ programs, you do have to be careful about
several differences between the two concerning stuff like
enums, type casting, etc.

---
William Ernest Reid

Mar 28 '07 #13
In article <sD************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote:
....
>object files pretty much at will. It is quite obvious that some people
here who are experts on the return value of main() don't understand
this.
Well said, sir! And, you might add, the ethics of casting the return
value of malloc().

Note (for the uninitiated): The "casting the return value of malloc()"
thing is an idea that is taken as gospel around here, but has no real
world traction outside of this NG. It is certainly possible to disagree
with this bit of dogma, but doing so will win you no friends around here.

Mar 28 '07 #14
Ian Collins wrote:
CBFalconer wrote:
>dolphin wrote:
>>Is it a good thing that program mix C and C++?

Mix, no. You can call C routines from C++, but not the reverse.

No so, C++ provides a mechanism (the extern "C" linkage specifier)
to make C++ functions callable from C.
That makes C callable from C++. Not the reverse.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 28 '07 #15
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:
>>>CBFalconer wrote:

dolphin wrote:

>Is it a good thing that program mix C and C++?

Mix, no. You can call C routines from C++, but not the reverse.
No so, C++ provides a mechanism (the extern "C" linkage specifier) to
make C++ functions callable from C.

No, it doesn't. It makes C functions callable from C++.
All together now: oh yes it does!
I can find no evidence in ISO/IEC 9899 to support your claim - or, for
that matter, my own claim that it makes C functions callable from C++.
I therefore withdraw that claim. Please provide chapter and verse from
ISO/IEC 9899 for extern "C" being a mechanism to C++ functions callable
from C, or alternatively follow my lead by withdrawing your claim.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 28 '07 #16
Richard Heathfield wrote:
>
Ian Collins said:
CBFalconer wrote:
dolphin wrote:

Is it a good thing that program mix C and C++?
Mix, no. You can call C routines from C++, but not the reverse.
No so, C++ provides a mechanism (the extern "C" linkage specifier) to
make C++ functions callable from C.

No, it doesn't. It makes C functions callable from C++.
It indirectly allows C to call C++. By compiling with C++, and
declaring a function extern "C", that particular function can call
C++ functions, and can be called from C functions.

At least that has been my experience. I can have legacy C code
call, for example, Microsoft's MFC functions via such a wrapper.

Of course, C++ is irrelevent to comp.lang.c, but C-to-C++ may be
relevent on comp.lang.c++.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Mar 28 '07 #17
Kenny McCormack wrote:
>
In article <sD************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
Bill Reid <ho********@hap pyhealthy.netwr ote:
...
object files pretty much at will. It is quite obvious that some people
here who are experts on the return value of main() don't understand
this.

Well said, sir! And, you might add, the ethics of casting the return
value of malloc().

Note (for the uninitiated): The "casting the return value of malloc()"
thing is an idea that is taken as gospel around here, but has no real
world traction outside of this NG. It is certainly possible to disagree
with this bit of dogma, but doing so will win you no friends around here.
It's considered bad practice, as it can hide a missing #include for
the malloc-and-friends prototypes. Without those prototypes, all
bets are off as to what happens when the compiler takes what it
thinks is an int return and casts it into your pointer. (This is
not just theoretical. I have worked on "real world" platforms where
pointers are returned differently than integers. Specifically, the
Motorola 680x0 series of CPUs have two sets of registers -- one for
"data" and one for "addresses" -- ie: D0, D1, D2 and so on, plus A0,
A1, A2 and so on. Pointers are returned in A0, and ints are
returned in D0. The value of D0 upon return from malloc is
unrelated to the pointer returned in A0, yet the missing prototype
will cause the compiler to assume that D0 contains the return.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Mar 28 '07 #18
CBFalconer <cb********@yah oo.comwrote:
# Ian Collins wrote:
# CBFalconer wrote:
# >dolphin wrote:
# >>
# >>Is it a good thing that program mix C and C++?
# >>
# >Mix, no. You can call C routines from C++, but not the reverse.
# >
# No so, C++ provides a mechanism (the extern "C" linkage specifier)
# to make C++ functions callable from C.
#
# That makes C callable from C++. Not the reverse.

That comes as a real shock to those of us who do so, by deviously
following the extern "C" rules explicitly intended to allow this.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Mar 28 '07 #19
CBFalconer wrote:
Ian Collins wrote:
>>CBFalconer wrote:
>>>dolphin wrote:
Is it a good thing that program mix C and C++?

Mix, no. You can call C routines from C++, but not the reverse.

No so, C++ provides a mechanism (the extern "C" linkage specifier)
to make C++ functions callable from C.

That makes C callable from C++. Not the reverse.
It's a two way street.

--
Ian Collins.
Mar 28 '07 #20

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

Similar topics

24
3617
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works, first and foremost. If it works well, even better. The same goes for ease of maintenance, memory footprint, speed, etc, etc. Most of the time, people are writing code for a use in the *real world*, and not just as an academic exercise. Look at...
36
4657
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am proposing a single column table with a field name called vehicle_type and this will contain the vehicle type. Sot it will be
39
2864
by: Suresh | last post by:
Hi, I am new to C and curious to know some basic questions about C. Why C is good? In what way its better than any other languages? If it is no longer a choice of programming language...why people still study it? And also its not an OO language either...is there any advantages in being not an OO language? I know one reason is that, IT started with C and so they are still using
113
12359
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same algorithm work with strings that may or may not be unicode 3) Number of bytes back must either be <= number of _TCHARs in * sizeof(_TCHAR), or the relation between output size and input size can be calculated simply. Has to take into account the...
13
2105
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
11
3810
by: Someonekicked | last post by:
I want to save tables of integers to files. One way to write the cells as fixed size in the file, is to use reinterpret_cast. Is that a bad choice, good choice? I remember once before I posted a program using it, and some suggested that I might get errors using it. are there any other ways to write the cells as fixed size in the file? (knowing the cells will only contain integers). here is how I plan to do it (without much caring...
63
3984
by: John Salerno | last post by:
I know there's a request for a good IDE at least once a week on the ng, but hopefully this question is a little different. I'm looking for suggestions for a good cross-platform text editor (which the features for coding, such as syntax highlighting, etc.) but not a full IDE with all the fancy jazz (GUI developer, UML diagrams, etc.). Ideally, it would be something I could even put on a flash drive and move from computer to computer, but...
6
1875
by: mast2as | last post by:
I have posted a few messages in the last few days about a project I am working on which is a quite simple parser. I ended up using the try/ catch structure as a general mechanism to control what's happening in the program when it finds a syntax error in the file it parses. I would like to know if this is 'an acceptable thing to do' from a programmation point of view, or if I should keep using it only for catch exception created withing the...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.