By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

passing pointer to instance of class

P: n/a
Hi, I am a little uncertain about the concept of passing a reference to a
class to another instance of a class. for instance I thought that the
following was ok:

Network network = Network();
Population pool = Population(& network);

.... but this doesnt seem to work.. however the following works...

Network * network = new Network();
Population pool = Polulation( network );

Can someone tell me what the difference between the two are. I think i am
still getting confused when i should be using 'new' or now, i.e.:

1) Object ball = Object();
2) Object * ball = new Object();

Any help and insight would be much appreciated!

Cheers,
John
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John C wrote:
Hi, I am a little uncertain about the concept of passing a reference to a
class to another instance of a class. for instance I thought that the
following was ok:

Network network = Network();
Just write:

Network network;

It's simpler and saves you a potential copy of the object.
Population pool = Population(& network);
Population pool(&network);

&network is not a reference. It's the address of network.
... but this doesnt seem to work..
What do you mean by "doesnt seem to work"? How does it not work? What
happens, and where?
however the following works...

Network * network = new Network();
Population pool = Polulation( network );
Population pool(network);
Can someone tell me what the difference between the two are.
The second allocates the object dynamically, the first doesn't.
Just out of my crystal ball: If you write that code into a funciton body,
the object in the first example will be destroyed when the function returns
or an exception is thrown. In the second example, the object will exist
until you explicitly delete it.
I think i am still getting confused when i should be using 'new' or now,
i.e.:

1) Object ball = Object();
Object ball;
2) Object * ball = new Object();

Any help and insight would be much appreciated!

Jul 22 '05 #2

P: n/a
John C wrote:

Hi, I am a little uncertain about the concept of passing a reference to a
class to another instance of a class. for instance I thought that the
following was ok:

Network network = Network();
Population pool = Population(& network);

That depends on what the argument type for the first argument
of the Population constructor is.

If it is:

class Population
{
public:
Population( Network* pNetWork );
};

Then the above is OK. Population wants the address of a Network object,
&network denotes the address of the object called 'network'.

(There is however another thing in your calling code.
Why are you creating temporary objects and use those to copy
construct the actual objects? There is no need for it

Network network;
Population pool( &network );

does the same thing. Depnding on the optimization skills of your
compiler, the later however may be much faster. And it is simpler
also.
)

If however ...

class Population
{
public:
Population( Network& network );
};

.... the constructor takes a reference to a network object, then it becomes

Network network;
Population pool( network );

The constructor wants a reference to an object, you pass an object
from which the reference can be taken.
... but this doesnt seem to work.. however the following works...
Please: Always tell us what 'doesn't seem to work' means. Is
it a compiler error (which one?), is it a runtime error, or what
else is it. Never simply say: It doesn't work. You don't visit
your doctor and simply tell him: "It hurts". Do you?

Network * network = new Network();
Population pool = Polulation( network );

Can someone tell me what the difference between the two are. I think i am
still getting confused when i should be using 'new' or now, i.e.:
You should not use new unless you absolutely need it.

That is:
* when you need to allocate during runtime because at compile
time you don't know the exact type of object or the amount
of objects (because you eg. need to react to user input) to
allocate
* you need to get control over the objects lifetime, because the
default lifetime is not what you need (object gets destoyed at
the end of the enclosing scope)

1) Object ball = Object();
Simply:
Object ball;

That object gets destroyed automatically whenever the scope
in which that object is defined, is left.
2) Object * ball = new Object();
The dynamically allocated object gets destroyed when you call
delete on a pointer to it.

...
delete ball;

Any help and insight would be much appreciated!


Don't use new, when there is no reason for it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a

"John C" <jo****@novatel.com> wrote in message
news:41******@dnews.tpgi.com.au...
Hi, I am a little uncertain about the concept of passing a reference to a
class to another instance of a class. for instance I thought that the
following was ok:

Network network = Network();
Population pool = Population(& network);
That is not a reference. That is "address of". Also, you have no "new"
operator there.
Can someone tell me what the difference between the two are. I think i am
still getting confused when i should be using 'new' or now, i.e.:

1) Object ball = Object();
2) Object * ball = new Object();


Don't use pointers unless you have to! If you have to use "new", then you have
to use pointers. Also, you have use pointers if you are calling someone else's
function and that function takes a pointer. But don't go around using pointers
just for the heck of it. Now, do you want to know when you have to use "new"?
:-)
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.