class vs struct???

Started by
7 comments, last by GameDev.net 18 years, 8 months ago
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?
Gary.Goodbye, and thanks for all the fish.
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.
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 ><

;)
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).
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]
Quote:Original post by Wavarian
EDIT: MaulingMonkey needs to die >< get a girlfriend

Fixed to be more insulting. And true.
LOL....thanks for the help guys.

MM...I know some nice gilrs...for nice read easy;)
Gary.Goodbye, and thanks for all the fish.
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.
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.

This topic is closed to new replies.

Advertisement