473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

trouble with assigment operators,s tl container and a reference

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

Jul 19 '05 #1
4 1769

"Dean Mitchell" <de***@tcl.co.nz> wrote in message
news:3f********@news.compass.net.nz...
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
Jul 19 '05 #2
Hi Dean,

"Dean Mitchell" <de***@tcl.co.nz> wrote in message
news:3f********@news.compass.net.nz...
.....
| 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,
--
Ivan Vecerina <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 19 '05 #3
Dean Mitchell wrote:
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.

--
Attila aka WW
Jul 19 '05 #4
"Dean Mitchell" <de***@tcl.co.nz> wrote in message
news:3f********@news.compass.net.nz...
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

--
ES Kim
Jul 19 '05 #5

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

Similar topics

20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
2
by: yopwojtek | last post by:
Hello All, i know from some tutorials, that copy constructor and assigment operator is not inherited from base to derived class. i think it make sense but i wrote a little example: class Base {...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
6
by: Larry Serflaten | last post by:
I am trying to add 2 points together, and I am not succeeding. It appears the docs say I need a point and a size, but even that fails in the copy of VS 2003 I have. Exactly what are they trying...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
10
by: Adrian | last post by:
Below is an example of the problem I am having. I don't understand how I can get the compiler to see deriv &operator=(const T &rhs). I am sure this is a common problem - any suggestions? ...
2
by: PaulH | last post by:
I have a std::vector< ICMP_ECHO_REPLY container that I'd like to be able to do such wonderful STL operations as max_element(), accumulate(), greater_equal(), etc... So, I decided I would create a...
10
by: Lorenzo Di Gregorio | last post by:
Hello, I've been using Python for some DES simulations because we don't need full C speed and it's so much faster for writing models. During coding I find it handy to assign a variable *unless...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.