Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializing a reference ?

Reply
Thread Tools

Initializing a reference ?

 
 
vivekian
Guest
Posts: n/a
 
      08-07-2006
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

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      08-07-2006
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!!!


 
Reply With Quote
 
 
 
 
Duane Hebert
Guest
Posts: n/a
 
      08-07-2006

"vivekian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
nacci
Guest
Posts: n/a
 
      08-07-2006

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

 
Reply With Quote
 
sarathy
Guest
Posts: n/a
 
      08-07-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing static reference (non-POD) member variables Grey Alien C++ 8 07-24-2007 06:07 PM
Conditionally initializing a const reference without making a copy JurgenvonOerthel@hotmail.com C++ 10 04-12-2007 12:50 PM
Initializing a class member variable which is a reference to an array amparikh@gmail.com C++ 5 03-04-2006 12:02 AM
Initializing class member variable with reference chris C++ 2 11-15-2005 03:52 AM
Initializing a non-const reference with non-lvalue Andrew Ward C++ 2 07-19-2005 06:53 AM



Advertisments