Thread: Best Programming Pratices with Constructors

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    122

    Best Programming Pratices with Constructors

    Hello !
    I'm learning C++ by myself and I found very different ways to use constructors.
    Which is the best practice?

    Example 1 .h :
    Code:
      Product(int barCode, string const &title,
                string const &author,string const &publisher, 
                int price );
    or

    Code:
     Product(int , string , string , string , int );
    .cpp

    Code:
    Product::Product(int barCode, string const &title,
                string const &author, string const &publisher,
                int price)
    : barCode_(barCode), title_(title), author_(author), 
      publisher_(publisher), price_(price){}
    or
    Code:
    Product::Product(int barCode, string const &title,
                string const &author, string const &publisher,
                int price)
    {
    barCode_ = barCode;
    title_ = title;
    author_ = author; 
    publisher_ = publisher;
    price_ = price;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is the risk that the parameter names in a forward declaration might go out of sync with those in the definition, but, generally, specifying the parameter names is better than not doing so.

    Where you have the choice, initialising member variables using the constructor's initialiser list is better than assigning to member variables in the constructor body.
    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
    Jun 2005
    Posts
    6,815
    A couple of observations based on your usage of a "string" type.

    Are you employing "using namespace std" at file scope in your header file? Then don't - that is worse practice than any of the alternatives with implementing constructors.

    Are you employing "using std::string" at file scope in your header file? That is less of a crime than "using namespace std" in your header file, but still not a particularly good idea.

    There are numerous posts around here, and elsewhere, explaining why.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by laserlight View Post
    There is the risk that the parameter names in a forward declaration might go out of sync with those in the definition, but, generally, specifying the parameter names is better than not doing so.

    Where you have the choice, initialising member variables using the constructor's initialiser list is better than assigning to member variables in the constructor body.
    Thank you. I'll try keep everything in parameter list.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by grumpy View Post
    A couple of observations based on your usage of a "string" type.

    Are you employing "using namespace std" at file scope in your header file? Then don't - that is worse practice than any of the alternatives with implementing constructors.

    Are you employing "using std::string" at file scope in your header file? That is less of a crime than "using namespace std" in your header file, but still not a particularly good idea.

    There are numerous posts around here, and elsewhere, explaining why.
    I use "using namespace std;"
    And usually I use string as string const & . Is this wrong?
    I'll search on forum. I know this is a long a discussion, but I don't know who is right

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marcoesteves
    I use "using namespace std;"
    The important next question: where? A using directive like that (and also using declaration) should not be used at file scope in a header file, but class definitions often go in header files.

    Quote Originally Posted by marcoesteves
    And usually I use string as string const & . Is this wrong?
    No, it is not wrong in itself. But if you are using that in a class definition in a header file, then you should qualify string as std::string instead of using a using directive.
    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. constructors
    By waqas94 in forum C++ Programming
    Replies: 14
    Last Post: 07-15-2013, 12:34 PM
  2. No Constructors
    By jjetson in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2012, 05:01 PM
  3. about constructors
    By student111 in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2012, 12:48 PM
  4. Help with constructors!
    By -Syntax in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2008, 10:23 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

Tags for this Thread