Like Tree1Likes
  • 1 Post By laserlight

C++ Don'ts that was good in C

This is a discussion on C++ Don'ts that was good in C within the C++ Programming forums, part of the General Programming Boards category; I am taking a class in C++ and not really learning very much but the basics. I am already an ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2,551

    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
    21,636
    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.
    stahta01 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,551
    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,261
    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%.

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, 04:15 AM
  2. is win good enough?
    By ElastoManiac in forum Windows Programming
    Replies: 17
    Last Post: 01-05-2006, 12: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, 06:10 AM
  5. it's all good
    By runtojesus in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 11:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21