473,587 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing pointer to instance of class

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
3 3271
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
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

"John C" <jo****@novatel .com> wrote in message
news:41******@d news.tpgi.com.a u...
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1717
by: Jeff | last post by:
I'm trying to pass a proxy class instance (SWIG generated) of CClass, to a python callback function from C++. The proxy class instance of CClass is created from a pointer to the C++ class CClass. Using the code below, I receive the error message: "AttributeError: 'PySwigObject' object has no attribute 'GetName'" The python callback function is being passed in through the clientdata
6
2287
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among several other objects, I'd like to know how this is handled commonly. Could someone elaborate on this? Another situation is some objects could be shared among several threads. How to handle it commonly?
4
4248
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are complementary to one another. So I proceeded to decompose the tasks that deal with user options into two functions. Each of the functions do something and towards the end they do supplementary tasks that depend on the server option. The whole things...
17
9364
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
6
3940
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
9
3337
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
3
2605
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static wrapper function. I would prefer to be able to pass the member function as a void* to the static wrapper but I suspect this may not even be possible. Another solution would be option 2 below but I can't figure out the syntax to call obj->*pFn(). ...
7
10384
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
7
3293
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
7852
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
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
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...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
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
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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.