passing pointer to instance of class

J

John C

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
 
R

Rolf Magnus

John said:
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;
 
K

Karl Heinz Buchegger

John said:
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.
 
J

jeffc

John C said:
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"?
:)
 

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,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top