Initialization of non-integral type in initialization list

Discussion in 'C++' started by anongroupaccount@googlemail.com, Dec 11, 2005.

  1. Guest

    class CustomType
    {
    public:
    CustomType(){_i = 0;}
    CustomType(int i) : _i(i) {}
    private:
    int _i;
    };

    class MyClass
    {
    public:
    MyClass() : _member(4){}
    private:
    CustomType _member;
    };

    Will _member be ever created before the initialization list in MyClass?
    Am I guarantueed that it will only get created that once?

    I have run some tests, but I can't tell if my results are
    implementation specific, or undefined behaviour. Is this the right way?
     
    , Dec 11, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > class CustomType
    > {
    > public:
    > CustomType(){_i = 0;}
    > CustomType(int i) : _i(i) {}
    > private:
    > int _i;
    > };
    >
    > class MyClass
    > {
    > public:
    > MyClass() : _member(4){}
    > private:
    > CustomType _member;
    > };
    >
    > Will _member be ever created before the initialization list in MyClass?
    > Am I guarantueed that it will only get created that once?
    >
    > I have run some tests, but I can't tell if my results are
    > implementation specific, or undefined behaviour. Is this the right way?


    Sorry for all of the questions today. I've been writing a lot of
    programs to fill up grey areas in my knowledge.

    A further question: Imagine CustomType is an ABC. There's no way I can
    initialize that in the initialization list, is there? I'd actually have
    to use a pointer in this case, and assign it a value from making a new
    concrete class down the inheritance chain, right?
     
    , Dec 11, 2005
    #2
    1. Advertising

  3. wrote:

    > > class CustomType
    > > {
    > > public:
    > > CustomType(){_i = 0;}


    Use Constructor initialization list
    CustomType() : _i(0) { } ;

    > > CustomType(int i) : _i(i) {}
    > > private:
    > > int _i;
    > > };
    > >
    > > class MyClass
    > > {
    > > public:
    > > MyClass() : _member(4){}
    > > private:
    > > CustomType _member;
    > > };
    > >

    Donot use names starting with an underscore - they are reserved for
    implementation.

    > > Will _member be ever created before the initialization list in MyClass?
    > > Am I guarantueed that it will only get created that once?


    the initialization list will initialize the member "_member" when
    constructor of the class CustomType is called. Space for _member is
    reserved when you instantiate the class. This is immediately followed
    by the constructor call to initialize the object. So , though the
    object creation and initialization take place at two distinct points in
    time, as far as the programmer is concerned, they are "atomic".

    Also, if a member is of a user-defined type and it is not initialized
    in the constructor initialization list, then the compiler calls the
    default constructor for that member before executing the code of the
    constructor.


    >
    > A further question: Imagine CustomType is an ABC. There's no way I can
    > initialize that in the initialization list, is there? I'd actually have
    > to use a pointer in this case, and assign it a value from making a new
    > concrete class down the inheritance chain, right?


    If CustomType is an absract base class, you cannot have an instance of
    CustomType as a member of MyClass. So you are right in pointing out
    that you would actually need a pointer there.

    HTH.
     
    Neelesh Bodas, Dec 11, 2005
    #3
  4. Ron Natalie Guest

    Neelesh Bodas wrote:

    > Donot use names starting with an underscore - they are reserved for
    > implementation.


    No they aren't. Only ones begining with underscore followed by an
    uppercase letter, or those in the global namespace. His member
    variables here are fine as far as the language is concerned.
     
    Ron Natalie, Dec 11, 2005
    #4
  5. Ron Natalie Guest

    wrote:

    >
    > Will _member be ever created before the initialization list in MyClass?
    > Am I guarantueed that it will only get created that once?
    >

    The initialization list is just a indication of the initializers that
    are used when the appropriate initialization occurs. It has no bearing
    on the order of initialization, regardless of the order of the
    inializers in the list, or by their absence.

    Construction always occurs in a definite order:

    1. Any virtual base classes are initialized for the most derived object.

    then

    2. Recursively, the any non-virtual bases are initialized, in the
    order they are declared in the class definition(i.e., after the
    colon that follows the class name, NOT the member initializer list).

    3. All non-static members are initialized in the order they are listed
    in the class definition.

    4. The body of the constructor is run.

    All the initializer list does is say that when that constructor is used,
    then those parameters are used to initialize the listed subobject when
    the time for that initialization occurs as specified above.
     
    Ron Natalie, Dec 11, 2005
    #5
  6. Guest

    Ron Natalie wrote:
    > Neelesh Bodas wrote:
    >
    > > Donot use names starting with an underscore - they are reserved for
    > > implementation.

    >
    > No they aren't. Only ones begining with underscore followed by an
    > uppercase letter, or those in the global namespace. His member
    > variables here are fine as far as the language is concerned.


    Strangely, almost all of the C++ code I've seen uses an underscore in
    private variables. Is this not actually a common thing to do?
     
    , Dec 11, 2005
    #6
  7. Ron Natalie Guest

    wrote:
    > Ron Natalie wrote:
    >> Neelesh Bodas wrote:
    >>
    >>> Donot use names starting with an underscore - they are reserved for
    >>> implementation.

    >> No they aren't. Only ones begining with underscore followed by an
    >> uppercase letter, or those in the global namespace. His member
    >> variables here are fine as far as the language is concerned.

    >
    > Strangely, almost all of the C++ code I've seen uses an underscore in
    > private variables. Is this not actually a common thing to do?
    >

    A lot of people use a trailing underscore. I tend to do neither.
    For me a well designed class doesn't require any special decoration
    on the member variables. Member variables aren't visible outside
    the class, and a class member function rarely touches a variable
    outside the class, so what's the point?
     
    Ron Natalie, Dec 11, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    3
    Views:
    437
    Jakob Bieling
    Nov 7, 2003
  2. Mike Hewson
    Replies:
    14
    Views:
    11,893
    Micah Cowan
    Jan 7, 2005
  3. joshc
    Replies:
    7
    Views:
    389
    DHOLLINGSWORTH2
    Feb 26, 2005
  4. Mihai Rusu
    Replies:
    1
    Views:
    312
    James Kanze
    Aug 8, 2008
  5. Replies:
    1
    Views:
    344
    Alf P. Steinbach
    Apr 3, 2009
Loading...

Share This Page