473,498 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing Object References...See Example Code!!

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);
}

Jul 23 '05 #1
3 1487
ri*******@latter.demon.co.uk wrote:
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

Jul 23 '05 #2
richar...@latter.demon.co.uk wrote:
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.

Jul 23 '05 #3
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

Jul 23 '05 #4

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

Similar topics

4
5532
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
4
4176
by: hardwareman | last post by:
Hi All, I've read that I should use references instead of pointers, and as I do have some suspicious casts floating around in my code, I thought it a good idea to remove them. Ultimate...
29
2266
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
12
2641
by: Mike Eaton | last post by:
Hi all, What do people regard as the best practice with respect to freeing object references when you're done with them? Some people I've worked with in the past suggested that if you create an...
9
7377
by: arthernan | last post by:
Say I pass an object ByRef. And I want to store a reference to that object. How do I do this? Arturo Hernandez
9
1346
by: DrConti | last post by:
Dear Python developer community, I'm quite new to Python, so perhaps my question is well known and the answer too. I need a variable alias ( what in other languages you would call "a pointer"...
6
2466
by: Shailen Sukul | last post by:
Observed a weird behaviour with object references. See code listing below: using System; using System.Collections.Generic; using System.Text; namespace PointerExceptionTest { /*
2
1824
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
3
4871
ADezii
by: ADezii | last post by:
In writing code you often need to retrieve or set properties of the various Forms, Reports, Controls, or other Objects in your application. Generally, you refer to these Objects with statements like:...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.