Thread: using c++ with c

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    using c++ with c

    my freind on msn said sometimes its acceptable(he says he's much smarter than me at this) to sometimes use c code with c++

    is this true and if so when but i dont think it is

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by lilhawk2892
    is this true and if so when
    When you don't want to reinvent the wheel.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by lilhawk2892
    my freind on msn said sometimes its acceptable(he says he's much smarter than me at this) to sometimes use c code with c++

    is this true and if so when but i dont think it is
    Well, in almost all cases, C++ has an improved alternative to the original C method of getting something done. When programming in C++, I rarely find any need to use the C method to do anything. Keep in mind, much of the libraries used in C are still used in C++, for instance <cmath> and <cctype>. Now, if you're porting C code to a C++ program, it's generally fine to leave the C as C and just build the C++ around it so long as the C still works. As they say, if it isn't broken, don't fix it.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thanks im gonna show this to him see what he says

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by SlyMaelstrom
    Well, in almost all cases, C++ has an improved alternative to the original C method of getting something done. When programming in C++, I rarely find any need to use the C method to do anything. Keep in mind, much of the libraries used in C are still used in C++, for instance <cmath> and <cctype>. Now, if you're porting C code to a C++ program, it's generally fine to leave the C as C and just build the C++ around it so long as the C still works. As they say, if it isn't broken, don't fix it.
    For many cases, yes, I'll agree. But there are libraries out there that are written in C: libcurl, SDL, pthreads, Win32 API, etc. Some of these have C++ wrappers/equivalents. And some of us use the C version anyways. I've intermixed C++ with all of the above libraries except pthreads (it's only a matter of time).

    Mixing C libraries with C++ code is, imho, perfectly acceptable and good thing. Mixing C and C++ code together in the same project, eh, it's ok. But if you're the one writing it, you might as well take advantage of the features C++ gives you.

    And the STL/C++ adds a lot of stuff to the binary. Especially when debugging - those suckers get big quick. (But with today's hard drives, this is moot. And it's debugging...) Though wxWindows is large no matter what...

    Edit: Missed your comment about libraries... oh well. My 2cent.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yeah, sometimes one uses C-style code when dealing with libraries or APIs that are written for C and C++ both. I'd never use C-style code within a C++ program in any other case, though. In general the C++ style is shorter and easier to understand.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed