Confused on class member variable initialization

Discussion in 'C++' started by Randy, Jan 12, 2006.

  1. Randy

    Randy Guest

    Hi,

    When a class contains another class as a member variable, e.g.,

    class ClassA
    {
    int a;

    public :

    ClassA(int a1, int a2){a = a1 - a2;};
    ~ClassA();
    };

    class ClassB
    {
    int b;
    ClassA classA;

    ....
    };

    what exactly happens to member variable classA when an instance of B is
    created? More
    specifically,

    1. How do you initialize classA within ClassB's constructor?

    2. What happens if you provide no initialization of classA
    within ClassB's constructor?

    --Randy
    Randy, Jan 12, 2006
    #1
    1. Advertising

  2. Randy

    TB Guest

    Randy sade:
    > Hi,
    >
    > When a class contains another class as a member variable, e.g.,
    >
    > class ClassA
    > {
    > int a;
    >
    > public :
    >
    > ClassA(int a1, int a2){a = a1 - a2;};
    > ~ClassA();
    > };
    >
    > class ClassB
    > {
    > int b;
    > ClassA classA;
    >
    > ...
    > };
    >
    > what exactly happens to member variable classA when an instance of B is
    > created? More
    > specifically,
    >
    > 1. How do you initialize classA within ClassB's constructor?
    >


    ClassB::ClassB() : classA(2,3) {

    }

    > 2. What happens if you provide no initialization of classA
    > within ClassB's constructor?
    >


    Error. Since ClassA doesn't have a default constructor it must
    be initialized.

    TB
    TB, Jan 12, 2006
    #2
    1. Advertising

  3. Randy

    Randy Guest

    Thanks TB. Is there an alternate syntax to

    ClassB::ClassB() : classA(2,3) {

    namely, one that can be used inside the body of
    the constructor? If one had numerous class
    member variables that required initializing, this
    could become ugly.

    I'm thinking it would be simply

    ClassB::ClassB()
    {
    classA(2,3);
    }

    no?

    --Randy
    Randy, Jan 12, 2006
    #3
  4. Randy

    TB Guest

    Randy sade:
    > Thanks TB. Is there an alternate syntax to
    >
    > ClassB::ClassB() : classA(2,3) {
    >
    > namely, one that can be used inside the body of
    > the constructor? If one had numerous class
    > member variables that required initializing, this
    > could become ugly.


    Provide a default constructor and then initialize
    it through a secondary interface.

    > I'm thinking it would be simply
    >
    > ClassB::ClassB()
    > {
    > classA(2,3);
    > }
    >
    > no?
    >


    No. When the execution point enters the constructor body the object
    is fully constructed. Only initialization occurs therein,
    despite its name. And, it's not that ugly =)

    TB
    TB, Jan 12, 2006
    #4
  5. Randy

    Jim Langston Guest

    "Randy" <> wrote in message
    news:...
    > Thanks TB. Is there an alternate syntax to
    >
    > ClassB::ClassB() : classA(2,3) {
    >
    > namely, one that can be used inside the body of
    > the constructor? If one had numerous class
    > member variables that required initializing, this
    > could become ugly.
    >
    > I'm thinking it would be simply
    >
    > ClassB::ClassB()
    > {
    > classA(2,3);
    > }
    >
    > no?


    No. If absolutely can't, or refuse, to use the initialization list, then
    use a pointer instead.

    class ClassB
    {
    ClassA* classA;
    ClassB() { classA = new ClassA(2, 3); };
    };

    But honestly, once you start using initialization lists it becomes more
    natural. I also at first didn't like them and would find ways around them,
    now I initialize EVERYTHING in my class in an initialization list.

    Although, to be honest, I would really like a cleaner way of doing this:

    CGUIElement::CGUIElement( std::string OpenFileName, std::string
    ClosedFileName,
    JCOLOR ColorKey,
    GUIType WhatType,
    unsigned long X, unsigned long Y,
    unsigned int inX1, unsigned int inY1,
    unsigned int inX2, unsigned int inY2,
    unsigned int textX, unsigned int textY,
    unsigned int textX2, unsigned int textY2) : OpenNotifier(this),
    OpenObserver(this),
    CloseNotifier(this), CloseObserver(this),
    CloseOnOpenNotifier(this), CloseOnOpenObserver(this),
    IsOpen(false), IsLoaded(false),
    NotifyMsg(0),
    Type(WhatType),
    BitmapX(X), BitmapY(Y),
    X1(inX1), Y1(inY1),
    X2(inX2), Y2(inY2),
    TextX(textX), TextY(textY),
    TextX2(textX2), TextY2(textY2),
    TextWidth(0), TextHeight(0)
    {
    ....

    especially when that's combined with the declaration:

    CGUIElement( std::string OpenFileName, std::string ClosedFileName,
    JCOLOR ColorKey,
    GUIType WhatType,
    unsigned long X, unsigned long Y,
    unsigned int ThisX1 = 0, unsigned int ThisY1 = 0,
    unsigned int ThisX2 = 0, unsigned int ThisY2 = 0,
    unsigned int textX = 0, unsigned int textY = 0,
    unsigned int textX2 = 0, unsigned int textY2 = 0);
    Jim Langston, Jan 13, 2006
    #5
  6. Randy

    JustBoo Guest

    On Thu, 12 Jan 2006 19:17:55 -0800, "Jim Langston"
    <> wrote:
    >Although, to be honest, I would really like a cleaner way of doing this:
    >
    >CGUIElement::CGUIElement( std::string OpenFileName, std::string


    [snipped function with many parameters]

    I'm not claiming this is better, just better for me. :)

    I do a few different things in this situation. One is I create a POD
    (struct) with all the parameters in it and pass that around.

    CGUIElement::CGUIElement( POD& params );

    I find I can then make my functions smaller by dividing and conquering
    the beast. It could add complexity though by having the POD updated
    and /or read in several different places. I've never found it a
    problem though.

    It's just to make the code look cleaner really. Obviously, the same
    amount of work (init and such) needs done somewhere.

    If I have a large amount of params to pass around I've also made them
    full-blown classes and then have some of the work (validation for
    example) done internal to the class itself. A Parameter class as it
    were. I think this follows a Design Pattern that I can't think of
    right now. :) In fact, I think it was a design pattern that gave me
    the idea.

    I understand with some API's this might not be possible.

    "Only in our dreams are we free. The rest of the time we need wages."
    - Terry Pratchett
    JustBoo, Jan 13, 2006
    #6
    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. E11
    Replies:
    1
    Views:
    4,716
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    3
    Views:
    832
    Peter_Julian
    Oct 10, 2005
  3. Replies:
    2
    Views:
    317
    Dennis Lee Bieber
    Nov 15, 2007
  4. Angus
    Replies:
    1
    Views:
    2,609
  5. aaragon
    Replies:
    2
    Views:
    606
    James Kanze
    Nov 2, 2008
Loading...

Share This Page