Initializing Object References...See Example Code!!

R

richard_l

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 created a
reference to that object in the class Container by using Value& m_Val.


At present the only way I can initialise this object is by initialising
the object on creation eg.

Container (Value& Val) : m_Value(Val) { ... }

Is there another way of initalizing this reference whithout using this
method?

Many thanks in advance,

Richard Latter


Code....


#include <iostream>

using namespace std;


class Value
{
public:
int& m_nVal;

public:
Value (int& nVal) : m_nVal(nVal) { }

public:
~Value () { }
};


class Container
{
public:
Value& m_Value;

Container (Value& Val) : m_Value(Val) { }
public:
~Container () { }
};



int main (void)
{
int i=5;
int& ri=i;
Value val(i);
Value &Vref=val;

Container cont(val);

cout << "i = " << i << endl;
cout << "Value = " << val.m_nVal << endl;
cout << "Container = " << cont.m_Value.m_nVal << endl;

i = 20;

cout << "i = " << i << endl;
cout << "Value = " << val.m_nVal << endl;
cout << "Container = " << cont.m_Value.m_nVal << endl;

return (0);
}
 
V

Victor Bazarov

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 created a
reference to that object in the class Container by using Value& m_Val.


At present the only way I can initialise this object is by
initialising the object on creation eg.

Container (Value& Val) : m_Value(Val) { ... }

Is there another way of initalizing this reference whithout using this
method?
[...]

No. Members that are references have to be initialised in the constructor
initialiser list. There is no other way.

V
 
R

Rapscallion

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 created a
reference to that object in the class Container by using Value& m_Val.
At present the only way I can initialise this object is by initialising
the object on creation eg.

Container (Value& Val) : m_Value(Val) { ... }

Is there another way of initalizing this reference whithout using this
method?

What is the problem you encounter? If you don't have a Value when you
create a Container then use a pointer instead of a reference. Pointers
can be reseated and NULL.

R.C.
 
D

davidrubin

In any case, you should *always* use a pointer in this context.
Pointers can be documented as "held" or "owned" (indicating which
object is reponsible for the pointed-to object). A referenced object
cannot be owned. Furthermore, in most cases where ownership of one
object is passed to another, a smart pointer should be used. /david
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top