• Advertisement

Archived

This topic is now archived and is closed to further replies.

Instances of Classes

This topic is 5714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I''ve been wondering for a while now why some people define their classes like CClass *Object; and others like CClass Object;. What is the major advantage to creating a pointer to the object, instead of just creating the object? I''ve always created instances without pointers... but then again i''ve made them all global. Should I use the ''CClass *Object;'' format instead?

Share this post


Link to post
Share on other sites
Advertisement
One reason is if you don''t want your class to be constructed at the point where it''s declared. This is more implementation-specific and can usually be worked around (by using a blank constructor and adding an initialization function).

If you have two interdependant classes (class COne is a member of class CTwo and vice-versa), you''ll end up with infinitely nested classes. By using pointers to the classes as members (so COne now has a pointer to CTwo as a member and vice-versa), you''ll have just pointers to each other which doesn''t cause infinite nesting. Again, however, this is usually implementation-specific and can be avoided.

Also, there is the difference in how they are allocated based on whether it''s a straight instance or a newed pointer. I''m not sure on the specifics but I think one gets allocated on the heap, the other on the stack and there''s likely a good reason to choose one over the other.

-Auron

Share this post


Link to post
Share on other sites
This is really a question for the beginners forum but why not answer it.

By using a pointer you can allocate the memmory for the object whenever you want and you can also deallocate it whenever you want. The advantage is you can manage your own memmory. Also when you''re passing parameter''s to a function, it is best to pas a pointer to an object instead of the object it self. Passing the object means that the entire object get''s copy''d on to the stack , where passing a pointer to an object you only have to copy a 32 bit value (the addres) on to the stack.

Btw keeping al your objects global is really bad.

Share this post


Link to post
Share on other sites
quote:
Original post by George2
Also when you''re passing parameter''s to a function, it is best to pas a pointer to an object instead of the object it self.

It would be better still to pass a reference.

Share this post


Link to post
Share on other sites
Just to make a note of it: there is no speed difference between copying an object to the stack and copying a mere 4-byte address (passing it to a function, this is).

The true benefit of using pointers yields itself in the ability to create a large array of pointers to possibly large objects for a potentially very dynamic use (eg the program may end up only using one or two of these objects) at a low memory cost - imagine the difference between 200 CObject* elements, each 4 bytes in size (800 bytes total) and 200 CObject elements, each possibly several hundred or thousands of bytes in size, easily totalling in more memory you''ll ever be able to afford to by for your computer! This, of course, is the case if you declare a fixed number of elements (either objects or pointers) and do not use a linked list (which is, in fact, a bunch of pointers), etc.

Hope this helps,
Crispy

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy
Just to make a note of it: there is no speed difference between copying an object to the stack and copying a mere 4-byte address (passing it to a function, this is).


So you are saying that copying a 10000 byte object is just as fast as copying 4 bytes? Hmm... interesting...



Share this post


Link to post
Share on other sites
Very interesting ;-)

If you want to pass large objects around, a reference (const if you don''t want the original to be modified) is probably the way to go.

But as usual, it''s your preference.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Thanks for all the replies.
quote:

This is really a question for the beginners forum but why not answer it.


Sorry. I consider myself pretty fluent at C++ and Direct X but I've learned through books and tutorials; never had a real school-type class on it, therefore some basic stuff that I never bothered to learn haunts me at times. I wasn't sure how complex of a question mine was. Sorry again.

Anyway, I understand much better now. Thanks alot for the replies.
-Sam

[edited by - SamCN on May 31, 2002 11:36:57 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
So you are saying that copying a 10000 byte object is just as fast as copying 4 bytes? Hmm... interesting...



Ah! Irony I sense! Benchmark it. Pass a pointer to an object to and an actual object to a function some 10 million times, time it, and compare the results...

If you have a good explanation, please tell me, because the results I got were practically identical...

Crispy

Share this post


Link to post
Share on other sites
Object pointers also let you do something like the following:

Say you have a base class for automobiles and you subclass cars and trucks from it. Let's also say you have a class for garage that has a method, void Park(CAutomobile *auto), that places an automobile into it's parking lot. Using pointers you can do:


      
CGarage garage;
CAutomobile *aCar, *aTruck;
aCar = new CCar();
aTruck = new CTruck();

// park 'em in the garage

garage.Park(aCar);
garage.Park(aTruck);

// and so on...



Without having to overload Park for every kind of automobile subclass.

It's been a while since I've actually done this, so I may be a bit rusty. If anyone sees anything wrong with this, please let me know.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 31, 2002 7:00:01 PM]

[edited by - Chem0sh on May 31, 2002 7:01:16 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan

Original post by George2
Also when you're passing parameter's to a function, it is best to pas a pointer to an object instead of the object it self.

It would be better still to pass a reference.

---------------end of SabreMan's quote ----------
(stupid quoting stuff)

What in your opinion makes a reference better than a pointer ?

Passing a pointer to an object is passing the objects addres, how does a reference differ from this, seeing a reference is basicly providing an alias for an object ?

[edited by - George2 on June 1, 2002 2:23:54 PM]

[edited by - George2 on June 1, 2002 2:24:38 PM]

[edited by - George2 on June 1, 2002 2:25:36 PM]

Share this post


Link to post
Share on other sites
A pointer can refer to nothing, while a reference can''t.
A pointer can easily refer to something invalid, while you really have to want to screw it when you try that with a reference.

Usually, functions that take a pointer are supposed to not cause an access violation if it is zero, while references indicate crucial arguments. Also, in the function itself, the null-checking can be skipped. In time-critical functions that could mean a pretty performance gain (besides the fact that it looks neater).

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy

Ah! Irony I sense! Benchmark it. Pass a pointer to an object to and an actual object to a function some 10 million times, time it, and compare the results...


You''re not serious, are you?

Share this post


Link to post
Share on other sites
About the benchmark or the ten million part? I am serious about the benchmark. As to the figure - go ahead try it ten times. Just remember to find a veeery slow computer...

I personally think occasianlly benching simple stuff like this is quite a bit of entertainment... Then again, maybe I''m the only one...

Crispy

Share this post


Link to post
Share on other sites

    
#include <iostream>


class Foo
{
public:
int a[1024];
};


void doCopyStuff(Foo t)
{
return;
}


void doPointerStuff(Foo* t)
{
return;
}


int main(int argc, char* argv[])
{
Foo t;

std::cout << "Starting 'pass by value'\n";

for(int i = 0; i < 10000000; ++i)
doCopyStuff(t);

std::cout << "Done!\n";
std::cout << "Starting 'pass by pointer'\n";

for(int i = 0; i < 10000000; ++i)
doPointerStuff(&t);

std::cout << "Done!\n";

return 0;
}

Guess which one takes ages, and which one is essentially instant? Then change a[1024] to a[128], and watch it run 10 times faster.

[edited by - Martee on June 1, 2002 6:25:36 PM]

Share this post


Link to post
Share on other sites
What hasn''t been mentioned in this discussion is what it means to copy an object onto the stack. If you don''t pass a pointer or reference to a function then the object is passed by value. That means the copy constructor is called to create a new instance of the object! That means any changes made to the object inside the function wont effect the object that was passed to the function (just like passing an int by value, which may have been your intention). The big problem is that the default copy constructor that the compiler generates for you doesn''t do a "deep" copy (i.e. it will copy pointer members, not what they point to). So if the object does have pointer members, then anything done to those members will effect the object that was passed to the function (which is almost certainly not what you wanted). If thats the case then you should supply a copy constructor that does do a deep copy, or pass the parameter as a const pointer or reference.

As for which way is faster. I can''t see how constructing a new object can be faster than simply pushing an address onto the stack, unless the object itself only has one 32 bit member.

Cheers,

Ryan.

Share this post


Link to post
Share on other sites
quote:
Original post by Crispy
About the benchmark or the ten million part? I am serious about the benchmark. As to the figure - go ahead try it ten times. Just remember to find a veeery slow computer...

I personally think occasianlly benching simple stuff like this is quite a bit of entertainment... Then again, maybe I''m the only one...

Crispy

Oh, I agree that you should benchmark before you optimize. However, it doesn''t take a profiler to realize that copying a pointer is worlds more efficient than copying a huge object.

Share this post


Link to post
Share on other sites
quote:
Original post by Saib
A pointer can refer to nothing, while a reference can''t.
A pointer can easily refer to something invalid, while you really have to want to screw it when you try that with a reference.

Usually, functions that take a pointer are supposed to not cause an access violation if it is zero, while references indicate crucial arguments. Also, in the function itself, the null-checking can be skipped. In time-critical functions that could mean a pretty performance gain (besides the fact that it looks neater).


Your first arguments maybe good, but optimizing time critical functions by eliminating one if-test isn''t gonna get you much of performance gain(nearly nothing). How long do you think it''s gonna take to do an if-test ?

In my opinion the -> operator is far neater then the . operator.

Share this post


Link to post
Share on other sites

  • Advertisement