Thread: Mixing C with C++ code

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the conclusion is:
    1. It's not TECHNICALLY wrong to mix C and C++ if the resulting code performs the task required.

    2. In a C++ educational situation, it is probably right to require that code that can within reason be solved in C++ _is_ solved in C++. There are situations where 5 lines of C becomes 15 lines of C++, then perhaps the C solution is actually more appropriate - sscanf() can be such a case, particularly if the input format is not designed for reading via C++ [e.g. it's got various formatting within the input itself, that scanf can accept and validate, but C++ cin doesn't without a fair bit of effort].

    Where I work, we mix third party drivers written in C with our OS's C++ code - because we are not going to spend 6 man-years rewriting a graphics driver providing OpenGL to the client software - by the time that is finished and working correctly with good performance, the graphics processor is probably "old". But that is of course large chunks of C code mixed with other large chunks of C++ code - mixing it with a few lines of one, then a few lines of the other is not quite the same thing.

    But both C and C++ are tools - they come in the C++ toolchest - so it's "ok" to use any of the tools. Part of learning to program is of course learning how to use which tool - just like a carpenter needs to learn when to use a broad chisel, a narrow chisel, a knife, a drill or a saw - they all do useful things, and you can SOMETIMES use one in place of the other, but one is most often better than another.

    --
    Mats
    Last edited by matsp; 04-25-2009 at 10:07 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JVene
    If one defines an object as the collection of data and functions that act on that data relative to a particular subject (a loose one, it seems), then non-member functions can be part of an object.

    Stroustrup points out that it's an error to place some functions inside a class simply because they are conceptually part of that class. If a function is applicable more generically, such that it would serve it's purpose for other objects or other contexts, placing it inside a class may limit it's usefulness (or re-usefulness).
    The idea of genericity aside, appropriate provision of non-member non-friend functions can improve encapsulation.

    Quote Originally Posted by JVene
    As I read the message is several of these texts I mentioned, the message is that "object oriented" isn't a function of the language constructs, but of the method of thought in design. This is partly what brings forward a school of thought that OOP and OOD can be implemented in C (and sometimes rather heated arguments result).
    Yes, that is the school of thought that I subscribe to.

    Quote Originally Posted by R.Stiltskin
    On the other hand, maybe matsp, brewbuck and grumpy have already said all that needs to be said about the original question...
    Yes, I feel that your question has been sufficiently, though perhaps not satisfactorily, answered
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. mixing code with declarations
    By robwhit in forum C Programming
    Replies: 12
    Last Post: 01-31-2008, 12:55 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM