Thread: C++ Don'ts that was good in C

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    C++ Don'ts that was good in C

    I am taking a class in C++ and not really learning very much but the basics.
    I am already an average C programmer.

    I would like to increase my knowledge of things that are wrong in C++ that was good in C programming. Not really looking for OOP related items.

    Example items: (That I know about already.)

    Do not use MACROS in most cases.
    Use inline functions instead of things like "#define ADD(x,y) (x+y)"
    Use constants "const" instead of "#define MAX_VALUE 10"
    (Note: I know that enums are sometimes better to use for constants in both C and C++)

    Try to avoid C-strings (char arrays) and use std:string instead.

    Edit: Avoid the use of C type cast instead use static_cast and the others.

    Tim S.
    Last edited by stahta01; 04-27-2011 at 12:54 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by stahta01
    Use inline functions instead of things like "#define ADD(x,y) (x+y)"
    Well, an inline function would not suffice to replace the function-style macro in your example: you would need a function template.

    I suggest reading C++ Coding Standards by Sutter and Alexandrescu. A few items in the book come to mind with respect to your question:
    13. Ensure resources are owned by objects. Use explicit RAII and smart pointers.
    18. Declare variables as locally as possible.
    72. Prefer to use exceptions to report errors.
    76. Use vector by default. Otherwise, choose an appropriate container.
    90. Avoid type switching; prefer polymorphism.
    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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    I suggest reading C++ Coding Standards by Sutter and Alexandrescu. A few items in the book come to mind with respect to your question:
    13. Ensure resources are owned by objects. Use explicit RAII and smart pointers.
    90. Avoid type switching; prefer polymorphism.
    Thanks for the feedback; I am going to have to do research to understand the above items you mentioned.
    I understand 13 (no idea how to use explicit RAII); but, I have no idea what "type switching" is as it applies to C/C++ (I would guess related to casting).

    Tim S.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Check this out, it helped me alot to understand the differences between C and C++: C++ tutorial for C users

    Or the C++ FAQ, which explains alot of C++ details: C++ FAQ.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    I have no idea what "type switching" is as it applies to C/C++ (I would guess related to casting).
    Casting is part of it. More generally, it means to avoid writing code that overtly determines the type of an object, and does things differently depending on what the type is, such as
    Code:
    switch (object->type())
    {
         case object_type1:
               ((type1 *)object)->something_for_type1();
               break;
         case object_type2:
               ((type2 *)object)->something_for_type2();
               break;
    }
    Instead use polymorphism (for example, a virtual do_something() member function in a base class that is overridden by each derived class). Once the polymorphic class is set up, the above can be done simply as
    Code:
        object->do_something();   // object is pointer to base, but actually may point to an instance of derived class
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you have to be good at math to be good at C/C++?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-07-2007, 05:15 AM
  2. is win good enough?
    By ElastoManiac in forum Windows Programming
    Replies: 17
    Last Post: 01-05-2006, 01:20 PM
  3. do you have to be good at math to be good at C++?
    By orion- in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2005, 10:29 AM
  4. What is C good for?
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-29-2002, 07:10 AM
  5. it's all good
    By runtojesus in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2001, 12:47 AM