Solved

Objects creating and passing objects to other objects?

Posted on 2001-06-13
7
165 Views
Last Modified: 2010-04-02
I am developing a model in C++.  One of the main sections of the model consists of two objects: nodes and packets.

My packet objects are really just glorified structures storing storing integer data.

The node class includes several public methods to manipulate it, as well as a private STL list member to store packets -- a buffer, if you will.  Nodes can do several things.  The two most important are: 1) storing packets given to it and 2) moving packets from itself to a given node.  They do this using the following methods:

void add_packet(Packet p);
void move_packet(Node destination);

The add_packet method simply adds p to the end of the STL list contained in the node.  It's the move_packet method that is giving me problems.  Originally, I thought I could simply make a copy of packet I wanted to move, add it to the destination node (using add_packet, of course) and then delete the node from the starting list (the list contained in the object handling the message).  However, this did not produce the desired results.

Looking into the bug I found that while I'm in the move_packet method, everything is fine -- the destination node contains what it's supposed to and at the proper time.  However, as soon as I exit the method, the packet object disappears.

I've fooled around with several ways of getting around this, all to no avail.  What am I missing?  IOW, How do I move object A from object 1 to object 2?
0
Comment
Question by:Kaimelar
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6187703
Is there a reason why you've asked this question 3 times?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6187709
OK, make that 4 TIMES!!!
0
 
LVL 32

Expert Comment

by:jhance
ID: 6187719
Please delete the DUPLICATES and get your points back.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Kaimelar
ID: 6187725
My apologizes for the duplicated questions -- refreshing my browser caused the question to be submitted multiple times.
0
 
LVL 3

Accepted Solution

by:
JackThornton earned 150 total points
ID: 6188905
If the method signature above is correct, I see a potential problem.

If you're passing "Node destination", you're passing a copy of the entire node by value. What you're modifying is the copy, not the original.

What you probably want to do is either "Node &destination" or "Node *pDestination" - that is, pass either a reference or a pointer to the original object. That way your manipulations are occuring on the object you expect them to modify.

- jack
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6189844
JackThornton is right.
I have 2 guesses regarding your packet class:
1. In the class there are data members that are pointers.
2. you did not implement copy constructor.

When you pass a copy of the object to the method, a shallow copy is been perform by the default copy constructor, i.e. only the VALUE of the pointer is been copy instead of the CONTENT of the pointed address.
So, you can do as JackThornton suggested, which is the right thing to do in most cases, or implement a copy constructor for the class.

--EC--
0
 

Author Comment

by:Kaimelar
ID: 6191338
JackThornton hit the nail on the head -- I'd been so obsessed with the way the method was working I didn't even think about the parameters I was passing into it.  Thanks to everyone who looked at this problem.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now