Problem with references

M

meekee

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?
 
V

Victor Bazarov

meekee said:
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
 
M

meekee

Victor said:
Don't assign. Initialise.


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?
 
V

Victor Bazarov

meekee said:
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?

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
 
V

Victor Bazarov

meekee said:
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
 
K

Kai-Uwe Bux

meekee said:
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
 
M

meekee

Victor said:
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!
 
M

meekee

meekee said:
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?
 
J

Joe Greer

meekee said:
...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
 
V

Victor Bazarov

Joe said:
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
 
J

Joe Greer

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
 
V

Victor Bazarov

Joe said:
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
 
A

Andre Kostur

Hm it works if I do:


1)
class A {
SomeType& test;


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

This is initialization.
but not if I do:

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

This is assignment.
So initializing in 1) differs from assigning in 2) as you pointed out,
thanks!

Yep. One is initialization, one is not.
 
J

Jonathan Lane

So initializing in 1) differs from assigning in 2) as you pointed out,
...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.
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top