Struct vs Class

S

Steve

I'll be the first to admit, I'm not entirely clear on the appropriate
usage of either.
From what I am reading in my books, a Struct and a Class are pretty
much the same, with the difference being, a Class can have private and
protected members, but a Struct everything is Public by default.

I laymans terms what would be an appropriate reason to choose a Struct
over a Class?

So why would one want to choose a Class over a Struct.

According to ISO, what are the differences?

The following code is from an opensource game "cheater" program, but
it is what piqued my interest.

typedef struct _GUILDS {
PVOID pOneEntryVTable;
BYTE UnknownByte0x0005;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0044;
DWORD UnknownValue0x0048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArray0x804c[0x200];
BYTE UnknownByteArray0x824c[0x40];
} GUILDS, *PGUILDS;

Would there have been ANY advantage to making this a class?

Strangely enough, this Struct eventually becomes part of a class, that
has 5 other structs, what would the advantage be to creating 6
structs, and then placing them into a class, rather than using classes
to begin with or Structs to end with?

Well thanks in advance for you patience and help.
 
D

David Harmon

On 17 Apr 2004 11:49:48 -0700 in comp.lang.c++, (e-mail address removed)
(Steve) wrote,
I laymans terms what would be an appropriate reason to choose a Struct
over a Class?

They are absolutely identical, except for the "public" versus "private"
default question.

I prefer to use "struct" whenever it is C-compatible plain old data, and
"class" whenever it employs any C++ enhancements.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[7.8] What's the difference between the keywords struct and class?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
K

Kevin Goodsell

Steve said:
I'll be the first to admit, I'm not entirely clear on the appropriate
usage of either.
From what I am reading in my books, a Struct and a Class are pretty
much the same, with the difference being, a Class can have private and
protected members, but a Struct everything is Public by default.

That is wrong.

First, 'Struct' and 'Class' are both identifiers in C++. They have
whatever meaning you give them. If I say

int Struct;

then 'Struct' is an object of type 'int'.

'struct' and 'class', on the other hand, are language keywords used for
defining a new type ('class' also has another meaning in a template
parameter list). They are exactly the same except for one thing: by
default, members of a class and class bases are private, while members
of a struct and struct bases are public by default.

Both can have private, protected, and public members, and both can be
used as private, protected, or public bases.
I laymans terms what would be an appropriate reason to choose a Struct
over a Class?

The only real reason that I've seen is convention. By convention,
structs often don't contain functions, and/or contain only public members.
So why would one want to choose a Class over a Struct.
Convention.


According to ISO, what are the differences?

Default access privileges, as I described earlier.
The following code is from an opensource game "cheater" program, but
it is what piqued my interest.

typedef struct _GUILDS {

This is not permitted. Identifiers that begin with an underscore
followed by an upper-case letter or another underscore are reserved for
the implementation for any use. C++ programs may not use identifiers of
that form. If they do, the behavior is undefined (if the program even
compiles, which it may very well not).

Also, the use of typedef for creating a new type is superfluous in C++.
class and struct tags are type names in C++ (and aliasing a pointer type
is usually a bad idea -- if you want a pointer, say it with '*' so
everyone can see that it's a pointer).
PVOID pOneEntryVTable;
BYTE UnknownByte0x0005;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0044;
DWORD UnknownValue0x0048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArray0x804c[0x200];
BYTE UnknownByteArray0x824c[0x40];
} GUILDS, *PGUILDS;

Would there have been ANY advantage to making this a class?

I don't see any.
Strangely enough, this Struct eventually becomes part of a class, that
has 5 other structs, what would the advantage be to creating 6
structs, and then placing them into a class, rather than using classes
to begin with or Structs to end with?

No advantage, unless you're worried about saving about 10 characters of
typing. But it looks to me like the above struct was coded to be C, not
C++. Possibly the code was converted to C++ from C, or written using
parts cannibalized from a C program, or maybe parts of it are used in
both C and C++ programs. There are no classes in C, so code that comes
from C or is shared between C and C++ cannot use classes.

-Kevin
 
S

Steve

I'll be the first to admit, I'm not entirely clear on the appropriate
usage of either.
From what I am reading in my books, a Struct and a Class are pretty
much the same, with the difference being, a Class can have private and
protected members, but a Struct everything is Public by default.

I laymans terms what would be an appropriate reason to choose a Struct
over a Class?

So why would one want to choose a Class over a Struct.

According to ISO, what are the differences?

The following code is from an opensource game "cheater" program, but
it is what piqued my interest.

typedef struct _GUILDS {
PVOID pOneEntryVTable;
BYTE UnknownByte0x0005;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0044;
DWORD UnknownValue0x0048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArray0x804c[0x200];
BYTE UnknownByteArray0x824c[0x40];
} GUILDS, *PGUILDS;

Would there have been ANY advantage to making this a class?

Strangely enough, this Struct eventually becomes part of a class, that
has 5 other structs, what would the advantage be to creating 6
structs, and then placing them into a class, rather than using classes
to begin with or Structs to end with?

Well thanks in advance for you patience and help.

Oddly enough this thread wasn't visible in my newsreader until after I
posted my question. Anyways I see the in the thread above, thank you.
 
D

Daniel T.

I'll be the first to admit, I'm not entirely clear on the appropriate
usage of either.
From what I am reading in my books, a Struct and a Class are pretty
much the same, with the difference being, a Class can have private and
protected members, but a Struct everything is Public by default.

I laymans terms what would be an appropriate reason to choose a Struct
over a Class?

Personally, I use a struct when no part of the type is private or
protected, otherwise I use class. So for example I would do:

template < typename arg1, typename arg2, typename result>
struct binary_function {
typedef arg1 first_argument_type;
typedef arg2 second_argument_type;
typedef result result_type;
};

template < typename tp >
struct plus: public binary_function< tp, tp, tp > {
tp operator()( const tp& x, const tp& y ) const {
return x + y;
}
};


rather than:

template < typename arg1, typename arg2, typename result>
class binary_function {
public:
typedef arg1 first_argument_type;
typedef arg2 second_argument_type;
typedef result result_type;
};

template < typename tp >
class plus: public binary_function< tp, tp, tp > {
public:
tp operator()( const tp& x, const tp& y ) const {
return x + y;
}
};

Note however the compiler sees to two example above as exactly
equivalent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top