473,624 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 UnknownByte0x00 05;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0 044;
DWORD UnknownValue0x0 048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArra y0x804c[0x200];
BYTE UnknownByteArra y0x824c[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 31570
On 17 Apr 2004 11:49:48 -0700 in comp.lang.c++, gr*********@yah oo.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 UnknownByte0x00 05;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0 044;
DWORD UnknownValue0x0 048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArra y0x804c[0x200];
BYTE UnknownByteArra y0x824c[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*********@yah oo.com (Steve) wrote in message news:<bb******* *************** ***@posting.goo gle.com>...
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 UnknownByte0x00 05;
BYTE Unknown0x0005[0x3f];
DWORD UnknownValue0x0 044;
DWORD UnknownValue0x0 048;
CHAR GuildName[MAX_GUILDS][0x40];
BYTE UnknownByteArra y0x804c[0x200];
BYTE UnknownByteArra y0x824c[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*********@yah oo.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
559
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
1703
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
1790
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello( ostream & os ) { return os << "regular: hello"; }
3
2205
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 { int intV; char charV;
4
1497
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
3120
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; public byte PhaseErrorCount;
3
1651
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 information on how to make a simple change to a value found in a structure stored in an array. I'm storing map squares in a 2D array of structures (have also tried with classes - I'm still unclear on the difference, and welcome recommendations) . The...
5
1335
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 unsafe void Form1_Load(object sender, EventArgs e) { MyStruct *ptr;
12
1215
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
1276
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 random access for the results? (unproduced) 30 Does this take a PEP, or just a patch submission?
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.