473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Struct vs Class

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.
Jul 22 '05 #1
4 31553
On 17 Apr 2004 11:49:48 -0700 in comp.lang.c++, gr*********@yahoo.com
(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/

Jul 22 '05 #2
Steve wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3
gr*********@yahoo.com (Steve) wrote in message news:<bb*************************@posting.google.c om>...
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.
Jul 22 '05 #4
gr*********@yahoo.com (Steve) wrote:
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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt | last post by:
Hi all, Suppose I have a bunch of similar structs/classes in a format like this: struct s1{ int arg1; char arg2; }; struct s2{ char arg1;
3
by: SteveM | last post by:
This is probably an easy answer but since I am fairly new to C++ for some reason I can't see it :-( Any help would be appreciated :-) I have a class: class AClass { public:
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
3
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test...
4
by: Mario | last post by:
Hy, I have the fallowing code and I don't understand why is not working ... in a header file: typedef void* cheie; typedef void* valoare; struct _Tabel; typedef _Tabel* Tabel;
2
by: Sheikko | last post by:
Hi, I have a class, cerated like a struc for some reasons, and I want to reset all values in it. public class MyClass { public byte Channel; public byte SatelliteID; public byte SyncFlags;...
3
by: jetweedy | last post by:
Hi, I'm trying to figure out how I can reassign values within an array of structures. I'm trying to write a really simple text game to learn some C++, and I can't for the life of me find...
5
by: zensunni | last post by:
Whenever I try making a pointer to a struct or class, I get this error: Cannot take the address of, get the size of, or declare a pointer to a managed type(MyStruct) Code: public...
12
by: christery | last post by:
read something on yoda by Jo I think... annoying ... not like C I think... thougth I got it but no... what am I missing... public struct S { public int i;} public class C { public int i;}
6
by: castironpi | last post by:
struct.Struct lets you encode Python objects into structured memory. It accepts a format string, and optionally a buffer and offset to/from which to read/write the structure. What do you think of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.