Thread: Initializing member arrays in constructors?

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Wanted to add one more option for Customer (if the validation is simple) picking 666 as a "sensible" default. I guess you might also use static const members instead of magic values.

    Code:
    class Customer
    {
        public:
            Customer(unsigned pw):
                m_pw( pw < 500 ? pw : 666) 
            {}
        //...
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A vector is pretty much always better than a dynamic array, since a vector is just a really good implementation of a dynamic array. If you find better performance with the dynamic array, chances are you aren't using the vector correctly.

    If you know at compile time the size of the array, then a statically sized array might provide some performance benefits (if you really need them), but that's not the problem being discussed in this thread.


    As far as the initializer list, if you have to do some checking on the validity of the input, then of course you would put that inside the body of the constructor. However, that is a lot more rare than you would think. Besides, it only applies to constructor parameters, and you often have a lot more member variables that are not initialized via those parameters.

    Even if you don't put the member variable in the initialization list, it will still be initialized there. For built-in types like int this does nothing, but for class types a default constructor will be called. So for consistency and clarity people generally put all memeber variable initialization into that list.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    I was reading this on linline functions and it suggests the very same thing for inlining. It's a good read if anyone in is inclined:
    http://www.cs.cmu.edu/~gilpin/c++/performance.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing a private array member
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2008, 10:44 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Initializing Objects with constructors
    By freddyvorhees in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2008, 07:11 AM
  4. Constructors for creating 2d arrays
    By Welshy in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2005, 03:50 PM
  5. Replies: 14
    Last Post: 03-17-2003, 10:07 AM