Initializing a reference ?

V

vivekian

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
 
P

Phlip

vivekian said:
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.
 
D

Duane Hebert

vivekian said:
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.
 
N

nacci

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
 
S

sarathy

vivekian said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top