Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with references

meekee
Guest
 
Posts: n/a
#1: Sep 27 '07


In class A I get a reference to 'SomeType':

class A {
SomeType test;


public:
A(SomeType& someT) {
test = someT;

}
...
...
};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?

Victor Bazarov
Guest
 
Posts: n/a
#2: Sep 27 '07

re: Problem with references


meekee wrote:
Quote:
In class A I get a reference to 'SomeType':
>
class A {
SomeType test;
>
>
public:
A(SomeType& someT) {
test = someT;
Don't assign. Initialise.
Quote:
>
}
..
..
};
>
But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change someT?
Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


meekee
Guest
 
Posts: n/a
#3: Sep 27 '07

re: Problem with references


Victor Bazarov wrote:
Quote:
meekee wrote:
Quote:
>In class A I get a reference to 'SomeType':
>>
>class A {
>SomeType test;
>>
>>
>public:
>A(SomeType& someT) {
>test = someT;
>
Don't assign. Initialise.
>
Quote:
>}
>..
>..
>};
>>
>But if I do operations on test the are not visible in the someT
> instance since test is a local copy. But how do I make test a
>"reference copy" of someT so when I change test I also change someT?
>
Let your 'test' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to 'test' will be
visible in the object referred to by 'someT', as well.
>
V

I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field. How
do I initialize a field member with an argument that first available in
the constructor?
Jonathan Lane
Guest
 
Posts: n/a
#4: Sep 27 '07

re: Problem with references


On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
Quote:
In class A I get a reference to 'SomeType':
>
class A {
//SomeType test;

SomeType& test;

Quote:
>
public:
A(SomeType& someT) {
test = someT;
>
}
..
..
>
};
>
But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?

Victor Bazarov
Guest
 
Posts: n/a
#5: Sep 27 '07

re: Problem with references


meekee wrote:
Quote:
Victor Bazarov wrote:
Quote:
>meekee wrote:
Quote:
>>In class A I get a reference to 'SomeType':
>>>
>>class A {
>>SomeType test;
>>>
>>>
>>public:
>>A(SomeType& someT) {
>>test = someT;
>>
>Don't assign. Initialise.
>>
Quote:
>>}
>>..
>>..
>>};
>>>
>>But if I do operations on test the are not visible in the someT
>> instance since test is a local copy. But how do I make test a
>>"reference copy" of someT so when I change test I also change
>>someT?
>>
>Let your 'test' member be a reference to SomeType, and initialise it
>in the initialiser list. The all chagnes you make to 'test' will be
>visible in the object referred to by 'someT', as well.
>>
>V
>
>
I have tried making the test member a reference to SomeType:
>
SomeType& test;
>
but then I get an error that its forbidden to initialize the field.
WHAT?
Quote:
How do I initialize a field member with an argument that first
available in the constructor?
Use the constructor initialiser list. Doesn't your C++ textbook
explain how to do that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


meekee
Guest
 
Posts: n/a
#6: Sep 27 '07

re: Problem with references


Jonathan Lane wrote:
Quote:
On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
Quote:
>In class A I get a reference to 'SomeType':
>>
>class A {
//SomeType test;
>
SomeType& test;
>
>
I have tried that but it gives the error:error: uninitialized reference
member ‘test’
Victor Bazarov
Guest
 
Posts: n/a
#7: Sep 27 '07

re: Problem with references


meekee wrote:
Quote:
Jonathan Lane wrote:
Quote:
>On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:
Quote:
>>In class A I get a reference to 'SomeType':
>>>
>>class A {
>//SomeType test;
>>
>SomeType& test;
>>
>>
>
I have tried that but it gives the error:error: uninitialized
reference member ‘test’
The solution: initialise it! The constructor initialiser list
is the only place for that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Kai-Uwe Bux
Guest
 
Posts: n/a
#8: Sep 27 '07

re: Problem with references


meekee wrote:
Quote:
>
>
In class A I get a reference to 'SomeType':
>
class A {
SomeType test;
>
>
public:
A(SomeType& someT) {
test = someT;
>
}
..
..
};
>
But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?
You have two options:

class A {

SomeType & test;

public:

A ( SomeType & someT )
: test ( someT )
{}

};


or

class A {

SomeType * test_ptr;

public:

A ( SomeType & someT )
: test_ptr ( &someT )
{}

};


The first option is more natural and conveys intend more clearly since using
a pointer in C++ usually indicates that you want to allow for null (whereas
in this class, it is an invariant that test_ptr != 0). The pointer version,
however, can be necessary when you need to implement an assignment
operator: a reference cannot be reseated but you can change the pointer.



Best

Kai-Uwe Bux
meekee
Guest
 
Posts: n/a
#9: Sep 27 '07

re: Problem with references


Victor Bazarov wrote:
Quote:
meekee wrote:
Quote:
>Victor Bazarov wrote:
Quote:
>>meekee wrote:
>>>In class A I get a reference to 'SomeType':
>>>>
>>>class A {
>>>SomeType test;
>>>>
>>>>
>>>public:
>>>A(SomeType& someT) {
>>>test = someT;
>>Don't assign. Initialise.
>>>
>>>}
>>>..
>>>..
>>>};
>>>>
>>>But if I do operations on test the are not visible in the someT
>>> instance since test is a local copy. But how do I make test a
>>>"reference copy" of someT so when I change test I also change
>>>someT?
>>Let your 'test' member be a reference to SomeType, and initialise it
>>in the initialiser list. The all chagnes you make to 'test' will be
>>visible in the object referred to by 'someT', as well.
>>>
>>V
>>
>I have tried making the test member a reference to SomeType:
>>
>SomeType& test;
>>
>but then I get an error that its forbidden to initialize the field.
>
WHAT?
>
Quote:
>How do I initialize a field member with an argument that first
>available in the constructor?
>
Use the constructor initialiser list. Doesn't your C++ textbook
explain how to do that?
>
V
Hm it works if I do:


1)
class A {
SomeType& test;


public:
A(SomeType& someT) test(someT) {}
...
...
};


but not if I do:

2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
...
...
};

So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!
meekee
Guest
 
Posts: n/a
#10: Sep 27 '07

re: Problem with references


meekee wrote:
Quote:
Victor Bazarov wrote:
Quote:
>meekee wrote:
Quote:
>>Victor Bazarov wrote:
>>>meekee wrote:
>>>>In class A I get a reference to 'SomeType':
>>>>>
>>>>class A {
>>>>SomeType test;
>>>>>
>>>>>
>>>>public:
>>>>A(SomeType& someT) {
>>>>test = someT;
>>>Don't assign. Initialise.
>>>>
>>>>}
>>>>..
>>>>..
>>>>};
>>>>>
>>>>But if I do operations on test the are not visible in the someT
>>>> instance since test is a local copy. But how do I make test a
>>>>"reference copy" of someT so when I change test I also change
>>>>someT?
>>>Let your 'test' member be a reference to SomeType, and initialise it
>>>in the initialiser list. The all chagnes you make to 'test' will be
>>>visible in the object referred to by 'someT', as well.
>>>>
>>>V
>>>
>>I have tried making the test member a reference to SomeType:
>>>
>>SomeType& test;
>>>
>>but then I get an error that its forbidden to initialize the field.
>>
>WHAT?
>>
Quote:
>>How do I initialize a field member with an argument that first
>>available in the constructor?
>>
>Use the constructor initialiser list. Doesn't your C++ textbook
>explain how to do that?
>>
>V
>
Hm it works if I do:
>
>
1)
class A {
SomeType& test;
>
>
public:
A(SomeType& someT) test(someT) {}
..
..
};
>
>
but not if I do:
>
2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
..
..
};
>
So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!

....the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
Tim Love
Guest
 
Posts: n/a
#11: Sep 27 '07

re: Problem with references


meekee <meekee@dd.comwrites:

Quote:
>...the only problem with this is that its no longer possible to have a
>default constructor
True. By chance I've recently done a page which deals with this kind
of stuff - just the basics

http://www-h.eng.cam.ac.uk/help/tpl/...structors.html

Joe Greer
Guest
 
Posts: n/a
#12: Sep 27 '07

re: Problem with references


meekee <meekee@dd.comwrote in news:46fbb940$0$90266$14726298@news.sunsite.dk:
Quote:
>
>
...the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
If you want to allow a default constructor, then you need something which can
be uninitialized. In other words a pointer.

joe
Victor Bazarov
Guest
 
Posts: n/a
#13: Sep 27 '07

re: Problem with references


Joe Greer wrote:
Quote:
meekee <meekee@dd.comwrote in
news:46fbb940$0$90266$14726298@news.sunsite.dk:
>
Quote:
>>
>>
>...the only problem with this is that its no longer possible to have
>a default constructor, or am I missing something?
>
If you want to allow a default constructor, then you need something
which can be uninitialized. In other words a pointer.
Or initialise the reference to something that can be meaningful for
a that type of initialisation, like a static object or a 'new'd one.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Joe Greer
Guest
 
Posts: n/a
#14: Sep 27 '07

re: Problem with references


"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fdgufk$qrg$1
@news.datemas.de:
Quote:
Joe Greer wrote:
Quote:
>meekee <meekee@dd.comwrote in
>news:46fbb940$0$90266$14726298@news.sunsite.dk:
>>
Quote:
>>>
>>>
>>...the only problem with this is that its no longer possible to have
>>a default constructor, or am I missing something?
>>
>If you want to allow a default constructor, then you need something
>which can be uninitialized. In other words a pointer.
>
Or initialise the reference to something that can be meaningful for
a that type of initialisation, like a static object or a 'new'd one.
>
V
True, but I never have understood pointer phobia. Pointers are perfect for
this situation.

joe
Victor Bazarov
Guest
 
Posts: n/a
#15: Sep 27 '07

re: Problem with references


Joe Greer wrote:
Quote:
"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fdgufk$qrg$1
@news.datemas.de:
>
Quote:
>Joe Greer wrote:
Quote:
>>meekee <meekee@dd.comwrote in
>>news:46fbb940$0$90266$14726298@news.sunsite.dk :
>>>
>>>>
>>>>
>>>...the only problem with this is that its no longer possible to
>>>have a default constructor, or am I missing something?
>>>
>>If you want to allow a default constructor, then you need something
>>which can be uninitialized. In other words a pointer.
>>
>Or initialise the reference to something that can be meaningful for
>a that type of initialisation, like a static object or a 'new'd one.
>>
>V
>
True, but I never have understood pointer phobia. Pointers are
perfect for this situation.
There is no phobia. It's the matter of having to check it every
time before dereferencing. Pointers can be null, references can't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Andre Kostur
Guest
 
Posts: n/a
#16: Sep 28 '07

re: Problem with references


meekee <meekee@dd.comwrote in news:46fbb895$0$90266$14726298
@news.sunsite.dk:
Quote:
Hm it works if I do:
>
>
1)
class A {
SomeType& test;
>
>
public:
A(SomeType& someT) test(someT) {}
..
..
};
This is initialization.
Quote:
but not if I do:
>
2)
class A {
SomeType& test;
public:
A(SomeType& someT) {
test=someT
}
..
..
};
This is assignment.
Quote:
So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!
Yep. One is initialization, one is not.
Jonathan Lane
Guest
 
Posts: n/a
#17: Sep 28 '07

re: Problem with references


So initializing in 1) differs from assigning in 2) as you pointed out,
Quote:
Quote:
thanks!
>
...the only problem with this is that its no longer possible to have a
default constructor, or am I missing something?
Indeed, references have these properties:
Cannot be null
Must be initialised when created
Cannot be reassigned

That is, a reference is an alias to a pre-existing object not an
object in its own right. Therefore it doesn't make sense to have any
sort of constructor since it's not an object per-se. Making it a
reference and initialising it in the initialisation list is the way to
achieve what you asked for. If you need to be able to reassign this/
delay initialisation then it should be a pointer or some sort of smart
pointer.

Closed Thread