Jump to content
  • Advertisement
Sign in to follow this  
P3D-BrandonR

Structs vs. Class objects ...

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

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Make sure that you delete anything you've created with new once you've finished using it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!