Jump to content
  • Advertisement

Archived

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

SamCN

Instances of Classes

This topic is 5894 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!