473,811 Members | 3,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s tring,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(point er?) 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 1790

"Dean Mitchell" <de***@tcl.co.n z> wrote in message
news:3f******** @news.compass.n et.nz...
Hi,

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

typedef std::map<std::s tring,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(point er?) 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.n z> wrote in message
news:3f******** @news.compass.n et.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(point er?) 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::s tring,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(point er?) 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.n z> wrote in message
news:3f******** @news.compass.n et.nz...
Hi,

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

typedef std::map<std::s tring,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(point er?) 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
1849
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 a bit of a problem in seeing what needs to happen here. I am just not sure how I do this. Will the overloading function recognize a < and a usual <? Do I do an IF (a.letters < b.letters){ return true }
2
1564
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 { protected: int* a_; public: Base& operator=(const Base& _toCopy) {
11
2607
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
6
1284
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 to say here? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingPointClassop_AdditionTopic.asp Can't I use: pt1 = pt2 + pt3
3
7171
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 thinking was: (a) create a div for the slider / scroll nub to move within (b) attach event handlers which, onmousedown, specify the slider/nub is moveable, and onmouseup, specify it's not (c) attach an event handler to the contaning div which,...
10
1792
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? #include <iostream> #include <vector> struct base
2
1725
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 class to contain that information and overload the necessary operators. The idea is that I should be able to do something like this: std::vector< ICMP_ECHO_REPLY container; ICMP_ECHO_REPLY reply; container.push_back( reply );
10
1514
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 it has been already assigned*: I've found that this is often referred to as "once" assigment. The best I could come up with in Python is:
5
13391
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 http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5556
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
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 we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.