Initilize data member

This is a discussion on Initilize data member within the C++ Programming forums, part of the General Programming Boards category; Here is the code: class Base { public: Base(); protected: int m_data1; private: int m_data2; }; Base::Base() :m_data1(0), m_data2(0) { ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Talking Initilize data member

    Here is the code:

    class Base
    {
    public:
    Base();
    protected:
    int m_data1;
    private:
    int m_data2;
    };

    Base::Base()
    :m_data1(0),
    m_data2(0)
    {
    }

    class Derived : public Base
    {
    public:
    struct score{
    int courses;
    int avg;
    };

    Derived();
    private:
    score m_score;
    int m_data3;
    };

    Derived:erived()
    :Base(),
    m_data3(0)
    {
    m_data1=10;
    m_score.courses=0;
    m_score.avg=0;
    }

    Question:
    If in Derived:erived(), initilize the data member as below:

    Derived:erived()
    :Base(),
    m_data1(10),
    m_score.courses(0),
    m_score.avg(0),
    m_data3(0)
    {
    }

    I got compiler errors. Anyone knows the rule here? m_data1, and m_score are derived class data member, why am I not allowed to initilize like this?

    Thanks for reply.
    Last edited by xstone; 09-23-2001 at 12:26 AM.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    the base constructor is automatically called when a var of type base is created there is no need to explicitly call it.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Wink

    I think here I am trying to initilize m_data1 to 10, not 0. So it still need to assign value. Also, not sure why structure object could be use assignment initilization.

    BTW, any body knows the advantage of assignment initilization than assignment in parentheses?

  4. #4
    Unregistered
    Guest
    assignment in parens is supposed to be faster, but assignment initialization is easier to follow and understand.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you mean the intialisation lists in class constructors as oposed to initialising the member variables in the body of the constructor, then if you're using non-built in types in your class then the initialisation list will be quicker.

    When an instance of your class is created the member variables are created first before any code in the body in your constructor is executed. Therefore if you have nested classes as member variables then their default constructor will be called(any you'll be in trouble if you don't have a default constructor). Then when the body of your constructor is executed the assignment operator or copy constructor will be called to update these nested classes with the values that you have supplied in your constructor.

    Using an initialisation list means that the appropriate nested class constructor can be called when these nested objects are created and prevents the extra assignment function/copy constructor.
    zen

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    Zen, if my understanding is correct, you are saying the following code is correct with better performance? Can u verify?

    class Base
    {
    public:
    Base();
    protected:
    int m_data1;
    private:
    int m_data2;
    };

    Base::Base()
    :m_data1(0),
    m_data2(0)
    {
    }

    class Derived : public Base
    {
    public:
    struct score{
    int courses;
    int avg;
    score();
    };

    Derived();
    private:
    score m_score;
    int m_data3;
    };

    Derived::score::score()
    :courses(0),
    avg(0)
    {
    }

    Derivederived()
    :Base(),
    m_score(),
    m_data3(0)
    {
    m_data1=10;
    }

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If all you are calling are default consructors and initialising built in types, I'm don't think that there would be any performance gain, but people still use the initialisation list out of force of habit regardless.

    There is no point in calling default constructors that take no arguments explicitly in the initialisation list because it'll get called when the size object is created in the Derived class.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a data member that has no default contructor?
    By symbiote in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2009, 04:06 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Data member passing
    By wirefree101 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2006, 01:41 PM
  4. Help With Member Data Input
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2004, 04:21 PM
  5. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM

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