Initializing a reference ?

Discussion in 'C++' started by vivekian, Aug 7, 2006.

  1. vivekian

    vivekian Guest

    Hi ,

    Have a class where a private member is a reference member which needs
    to be initialized when the constructor is called. The compiler rightly
    will not allow this. Is there some way to postpone the initialization
    till the constructor is called or is this where pointers should be
    used.

    thanks in advance,
    vivekian
     
    vivekian, Aug 7, 2006
    #1
    1. Advertising

  2. vivekian

    Phlip Guest

    vivekian wrote:

    > Have a class where a private member is a reference member which needs
    > to be initialized when the constructor is called. The compiler rightly
    > will not allow this. Is there some way to postpone the initialization
    > till the constructor is called or is this where pointers should be
    > used.


    Prefer references unless you need a pointer's extra features.

    One feature is the ability to be re-seated. You need a pointer.

    Try this pattern:

    class NullClass: public MyClass {
    public:
    void whatever() {}
    void whatever2() {} // all methods do nothing
    };
    static NullClass aNullObject;

    class ClientClass
    {
    MyClass * pObject;
    public:
    ClientClass(): pObject(&aNullObject) {}
    void setObject(MyClass & anObject)
    {
    pObject = & anObject;
    }
    ....
    };

    Using that pattern, you get one of the benefits that the reference would
    have provided. You needn't say 'if(pObject)' before every call to
    pObject->whatever().

    This is more than just syntactic sugar. Always seek ways to break
    dependencies between objects. This pattern makes ClientClass, and everything
    it calls, less dependent on MyClass.

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
     
    Phlip, Aug 7, 2006
    #2
    1. Advertising

  3. vivekian

    Duane Hebert Guest

    "vivekian" <> wrote in message
    news:...
    > Hi ,
    >
    > Have a class where a private member is a reference member which needs
    > to be initialized when the constructor is called. The compiler rightly
    > will not allow this. Is there some way to postpone the initialization
    > till the constructor is called or is this where pointers should be
    > used.


    Use the initializer list.
     
    Duane Hebert, Aug 7, 2006
    #3
  4. vivekian

    nacci Guest

    vivekian a scris:
    > Hi ,
    >
    > Have a class where a private member is a reference member which needs
    > to be initialized when the constructor is called. The compiler rightly
    > will not allow this. Is there some way to postpone the initialization
    > till the constructor is called or is this where pointers should be
    > used.
    >
    > thanks in advance,
    > vivekian


    yes, use initializer list :p
     
    nacci, Aug 7, 2006
    #4
  5. vivekian

    sarathy Guest

    vivekian wrote:
    > Hi ,
    >
    > Have a class where a private member is a reference member which needs
    > to be initialized when the constructor is called. The compiler rightly
    > will not allow this. Is there some way to postpone the initialization
    > till the constructor is called or is this where pointers should be
    > used.
    >
    > thanks in advance,
    > vivekian


    Hi,
    I am not an expert in C++. But i guess that reference variables
    need not be initialized in the following three places.

    1. When it is a class member.
    2. When used as function parameters.
    3. When used as return value

    I guess the first to be your case. Then the compiler should not
    complain right???
    Please correct me if i am wrong.

    Regards,
    Sarathy
     
    sarathy, Aug 7, 2006
    #5
    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. Andrew Ward
    Replies:
    2
    Views:
    1,087
    Zorro
    Jul 19, 2005
  2. chris
    Replies:
    2
    Views:
    1,172
    Kwan Lim
    Nov 15, 2005
  3. Replies:
    5
    Views:
    507
    Alf P. Steinbach
    Mar 4, 2006
  4. Replies:
    10
    Views:
    490
    Zeppe
    Apr 12, 2007
  5. Grey Alien
    Replies:
    8
    Views:
    395
    Alf P. Steinbach
    Jul 24, 2007
Loading...

Share This Page