Jump to content
  • Advertisement
Sign in to follow this  
garyfletcher

class vs struct???

This topic is 4843 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 general question about structures. I'm reliably informed taht the only difference between structs and classes are their respective default class member scope (public and private). So, can structs be used as template:
template <typename T>
struct aStruct
{
    T* obj;

    ....etc....
}
Is that possible? Also I am curious as to when you would prefer using one as opposed to the other..is it just all to do with personal preference?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by garyfletcher
I have a general question about structures.

I'm reliably informed taht the only difference between structs and classes are their respective default class member scope (public and private).


Also the default inheritence scope. E.g. these two formations are equivilant:

struct derived : base {
type member;
};

class derived : public base {
public:
type member;
};

Quote:
So, can structs be used as template

Yes.
Quote:
Also I am curious as to when you would prefer using one as opposed to the other..is it just all to do with personal preference?

Yes, and/or the preference/guidelines of/set by your company/current project co-programmers.

A common preference is to use struct to indicate the formation is "plain old data", as in C where all structs were such, and to use the class keyword (intoduced with C++) otherwise.

Share this post


Link to post
Share on other sites
Yes you can use templates with structs.

And using one over the other simply depends on your coding style. I personally use them to group common variables together, but again, to each his/her own.

EDIT: MaulingMonkey needs to die ><

;)

Share this post


Link to post
Share on other sites
The default access specifier and the default inheritance type are both public for a struct and private for a class.

Those are the only differences, so yes - a struct can be a template.


Most people use class to represent classes and structs to represent data.

For example a pixel would usually be represented by a struc - something like this:
<code>
struct pixel
{
byte r,g,b;
};
</code>

But an image would probably be represented by a class that holds a vector of pixels (or something similar).

Share this post


Link to post
Share on other sites
Yes, they can be used as templates.
You'd rather use class if you have a "real" class with behaviour, where the data members are private, and struct otherwise, be it for dumb data holders (e.g. struct rgb) or for type traits templates etc.

[edit]Oh, yeah... ;)[/edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher
LOL....thanks for the help guys.

Anytime.
Quote:
MM...I know some nice gilrs...for nice read easy;)

Ehh, I'm more interested in a relationship than nookie. Thanks for the offer though.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I usually try to separate structs from classes by the notation they would not be independent objects but merely data chunks used by others. As such, I never include any functions into a struct (as a C struct would work), and always keep its members public.

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!