Skip to main content
gamedev.net
🔒 Locked

Structs vs. Class objects ...

Started by P3D-BrandonR Aug 29, 2005 at 7:46 AM 6 replies 950+ views
Original Post
P3D-BrandonR
P3D-BrandonR
I have a couple of basic C++ questions, along with 1 relating it back to game programming. That is my reason for posting these in this board verses the normal programming board. First Question: Is there a difference or a benefit in using a class object verses say a struct or typedef struct? I have some general objects that are singleton based that I have been using a typedef struct on and just using that verses a class object. Is there a benefit between using one vs. the other, or is it just personal preference? Second Question: When should a “new” statement be used on an object? I am having trouble figuring out when and why you should use this statement to declare a new instance or new object. For example: cMyObject cThisObject = new cMyObject(); or cMyObject cThisObject; Should a “new” statement be used when declaring it outside a class declaration? Also, what about structs, when should you use the “new” statement on them or should you use them at all? Third Question (tying back in to game programming): Is there a benefit in using structs over class objects in games? Does it matter? I geuss this question may just be opinionated only in why using one over the other. Conclusion: I fully appreciate the responses to these questions. I’m trying to fully understand certain areas that I can not find info on, or the kind of answers I need to fully understand. Like I said, pretty generic questions above but none the less questions I need help with =). Good day to all, Brandon
SiCrane
SiCrane
1) Spelling, and classes have default private access and inheritance while structs have default public access and inheritance.

2) If you need to have the instance of the class outlive the enclosing scope, new it. If what you want to do with the instance can be done within the enclosing scope, declare it as an auto variable.

3) class takes one less letter to type
P3D-BrandonR
P3D-BrandonR
SiCrane:

Thanks, I believe that gives me a better understanding of it all now.

=)
dmatter
dmatter
1)
A struct is a class that can be made compatible with C (struct is left-over for compliancy with C).
A struct defaults all access to public, whilst a class defaults all access to private
class my_class{public://...};

The above class is equivalent to the following struct:
struct my_class{};

The same applies to inheritance also (A struct defaults to public inheritance and a class defaults to private inheritance).

Using a class 'looks' better and is used by most people most of the time, except for perhaps small classes such as functors often 'look' better as structs IMO.

2)
These symbols specify a range of scope { and } you will hopefully recognize them [smile].
An instance of an object such as your cMyObject cThisObject cannot outlive the scope it declared in.
An instance created with new can outlive its scope by passing around a pointer to the object instead: cMyObject* pThisObject = new cMyObject.
Note the use of the pointer when using new.

3)
There is no benefit in most cases, people do expect to be looking at classes rather than structs but either way keep it consistant to keep things looking nice.
Max_Payne
Max_Payne
Personally, I prefer to use structs in the traditional C meaning (a group of public variables). If I need a constructor, operators and methods, I will use a class. I typically use structs to group variables inside of a class, for example. A bit like what you would use an std::pair<> for.

Looking for a serious game project?
www.xgameproject.com
P3D-BrandonR
P3D-BrandonR
Great explanations guys, I really appreciate the feedback. Really helpful stuff !

Brandon
smr
smr
Make sure that you delete anything you've created with new once you've finished using it.
JohnBolton
JohnBolton
Don't use "typedef struct { ... } foo;" in C++. It is a C hack that is unnecessary in C++.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.