473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Initializing a reference ?

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

Aug 7 '06 #1
5 1877
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!!!
Aug 7 '06 #2

"vivekian" <vi********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr 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.
Aug 7 '06 #3

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

Aug 7 '06 #4

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

Aug 7 '06 #5

sarathy wrote:
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???
It is the first case. The compiler does complain about uninitialized
reference member. Philip's solution does take care of the problem.

Aug 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
3
by: richard_l | last post by:
Hello All, Please see attached example code. I'm writing a program whereby I would like to reference an object from another object. For example I have an object called Value and I have...
2
by: Andrew Ward | last post by:
The following program compiles and runs fine on my compiler (vc7.1): #include <memory> using namespace std; class X {}; auto_ptr<X> foo() {
5
by: amparikh | last post by:
I have some test code which demonstrates the problem. I know I could solve this by just returning a pointer, but I better use a reference. In real code, what I actually want to return is a...
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.