trouble with assigment operators,s tl container and a reference

D

Dean Mitchell

Hi,

I have a simple STL map container that maps a stl string to a structure as
follows.

typedef std::map<std::string,TASKLIST> PCLIST;

In a class I have a member variable that is defined as a reference to a
PCLIST;
PCLIST &m_List;

In one of the member functions I pass in a reference to another PCLIST and I
then assign this to the m_List.
void class::SetList(PCLIST &NewList)
{
m_List = NewList;
}

Now my problem is that this copies the contents of the NewList to the m_List
so I now have two distinct PCLISTs, rather than m_List being a
reference(pointer?) to the original list. How can I make m_List just point
to the original PCLIST? Am I using references correctly? or should I just
use pointers instead?

I would really appreciate someone pointing me in the right direction.
Dean Mitchell
 
E

ES Kim

Dean Mitchell said:
Hi,

I have a simple STL map container that maps a stl string to a structure as
follows.

typedef std::map<std::string,TASKLIST> PCLIST;

In a class I have a member variable that is defined as a reference to a
PCLIST;
PCLIST &m_List;

In one of the member functions I pass in a reference to another PCLIST and I
then assign this to the m_List.
void class::SetList(PCLIST &NewList)
{
m_List = NewList;
}

Now my problem is that this copies the contents of the NewList to the m_List
so I now have two distinct PCLISTs, rather than m_List being a
reference(pointer?) to the original list. How can I make m_List just point
to the original PCLIST? Am I using references correctly? or should I just
use pointers instead?

I would really appreciate someone pointing me in the right direction.
Dean Mitchell

References and const objects should be initialized when they are
constructed since there's no chance after that. If you assign an
object to a reference, it just copies the rhs to the object being
referenced as what you have described.
There's no way to reseat a reference. Pointer fits better in this
case
 
J

John Harrison

Dean Mitchell said:
Hi,

I have a simple STL map container that maps a stl string to a structure as
follows.

typedef std::map<std::string,TASKLIST> PCLIST;

In a class I have a member variable that is defined as a reference to a
PCLIST;
PCLIST &m_List;

In one of the member functions I pass in a reference to another PCLIST and I
then assign this to the m_List.
void class::SetList(PCLIST &NewList)
{
m_List = NewList;
}

Now my problem is that this copies the contents of the NewList to the m_List
so I now have two distinct PCLISTs, rather than m_List being a
reference(pointer?) to the original list. How can I make m_List just point
to the original PCLIST? Am I using references correctly? or should I just
use pointers instead?

I would really appreciate someone pointing me in the right direction.
Dean Mitchell

You got it, you should use pointers instead.

References cannot be 'reseated'. When a reference is initialised it will
refer to the same object for the rest of its lifetime.

john
 
I

Ivan Vecerina

Hi Dean,

.....
| In one of the member functions I pass in a reference to another PCLIST and
I
| then assign this to the m_List.
| void class::SetList(PCLIST &NewList)
| {
| m_List = NewList;
| }
References cannot be 'assigned to' as you are trying to do.
A reference can only be initialized with an object, to which it will
always refer. Any assignment will modify the object being referred to,
as you reported.

Example:
int a = 5;
int b = 8;
int& r = a; // initialization: r refers to a (or "is an alias for a").
r = b; // now: a==8 , and still &r==&a

| Now my problem is that this copies the contents of the NewList to the
m_List
| so I now have two distinct PCLISTs, rather than m_List being a
| reference(pointer?) to the original list. How can I make m_List just
point
| to the original PCLIST? Am I using references correctly? or should I just
| use pointers instead?
You can either:

- specify the target list in the constructor of your class:
MyClass( PCLIST &NewList )
: m_List(NewList) // initializes the reference
{ }
This is the preferred approach if each instance of 'MyClass'
manipulates a single list.

- use a pointer instead of a reference, and keep a SetList method.
This makes sense if instances of MyClass may have to refer
to multiple lists, or to no list at all ( --> null pointer ).

Unless you cannot do without a SetList method, the first approach
should be preferred.


I hope this helps,
 
A

Attila Feher

Dean said:
Hi,

I have a simple STL map container that maps a stl string to a
structure as follows.

typedef std::map<std::string,TASKLIST> PCLIST;

It is a bad idea to use all caps for typedefs. They are traditionally used
for preprocessot macros and those beasts have no scope.
In a class I have a member variable that is defined as a reference
to a PCLIST;
PCLIST &m_List;

Very bad idea. References can only be _initialized_ once and then they will
refer to the same thing during their lifetime. So for example you cannot
make an assignment operator for a class with such a member.
In one of the member functions I pass in a reference to another
PCLIST and I then assign this to the m_List.
void class::SetList(PCLIST &NewList)

You have never tried to compile this code, am I right? (Hint: class is a
keyword)
{
m_List = NewList;
}

References can only be initialized. This thing will not do what you intend
it to do.
Now my problem is that this copies the contents of the NewList to the
m_List so I now have two distinct PCLISTs, rather than m_List being a
reference(pointer?) to the original list.

Not to the m_List but to the PCLIST referenced by the m_List.
How can I make m_List just
point to the original PCLIST?

If you want to *point* to somewhere what kind of type would you use?
Am I using references correctly?
Nope.

should I just use pointers instead?

It seems so.
I would really appreciate someone pointing
me in the right direction.

:) Pointing everywhere. :)

One important issue with such classes is: who is the owner of that list? Is
the list dynamically allocated? If not: what if your class instance
survives the list? The pointer will be pointing to... well who knows what.
 

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,141
Messages
2,570,814
Members
47,359
Latest member
Claim Bitcoin Earnings. $

Latest Threads

Top