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!!!