Connecting Tech Pros Worldwide Help | Site Map

Struct vs Class

  #1  
Old July 22nd, 2005, 10:48 AM
Steve
Guest
 
Posts: n/a
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.
  #2  
Old July 22nd, 2005, 10:48 AM
David Harmon
Guest
 
Posts: n/a

re: Struct vs Class


On 17 Apr 2004 11:49:48 -0700 in comp.lang.c++, gr82meetu78@yahoo.com
(Steve) wrote,[color=blue]
>I laymans terms what would be an appropriate reason to choose a Struct
>over a Class?[/color]

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/

  #3  
Old July 22nd, 2005, 10:48 AM
Kevin Goodsell
Guest
 
Posts: n/a

re: Struct vs Class


Steve wrote:
[color=blue]
> 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.[/color]

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.
[color=blue]
>
> I laymans terms what would be an appropriate reason to choose a Struct
> over a Class?[/color]

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

Convention.
[color=blue]
>
> According to ISO, what are the differences?[/color]

Default access privileges, as I described earlier.
[color=blue]
>
> The following code is from an opensource game "cheater" program, but
> it is what piqued my interest.
>
> typedef struct _GUILDS {[/color]

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).
[color=blue]
> 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?[/color]

I don't see any.
[color=blue]
>
> 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?[/color]

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
  #4  
Old July 22nd, 2005, 10:48 AM
Steve
Guest
 
Posts: n/a

re: Struct vs Class


gr82meetu78@yahoo.com (Steve) wrote in message news:<bb8f15bd.0404171049.becd501@posting.google.c om>...[color=blue]
> 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.[/color]

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.
  #5  
Old July 22nd, 2005, 10:48 AM
Daniel T.
Guest
 
Posts: n/a

re: Struct vs Class


gr82meetu78@yahoo.com (Steve) wrote:
[color=blue]
> 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?[/color]

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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct vs Class JohnGoogle@hotmail.co.uk answers 37 November 3rd, 2006 01:55 AM
Struct VS Class Chua Wen Ching answers 5 November 16th, 2005 07:36 AM
Struct vs. class as simple container Wellu Mäkinen answers 7 July 22nd, 2005 11:16 AM