Skip to main content
GameDev.net gamedev.net
🔒 Locked

Struct vs. Class

Started by Hybrid Mar 1, 2003 at 7:06 AM 32 replies 900+ views
Original Post
Hybrid
Hybrid
When using C++ is there any advantage/disadvantage of using a class with no member functions just public data, over using a struct? I mean you can add stuff easily to a class if somthing pops up in the future, like the need to add a member function. But is there anything really different between the following?
typedef struct tData_tag
{
    int someData;
} tData;
 
and...
class CData
{
public:
     int someData;
}
 
JuNC
JuNC
For the hundred millionth time no.

struct and class are exactly equivalent except that struct has default public access.

(also note in C++ you don''t need the typedef struct, just do:

struct tData
{
int someData;
};

)
Dactylos
Dactylos
In C++ a struct and a class are exactly alike except that classes default to private inheritance and private access for members, while structs default to public for both inheritance and access. In other words, it won''t matter which one you choose to use, since you can use both in exactly the same way.
George2
George2
quote:
Original post by Hybrid
When using C++ is there any advantage/disadvantage of using a class with no member functions just public data, over using a struct? I mean you can add stuff easily to a class if somthing pops up in the future, like the need to add a member function. But is there anything really different between the following?






classes and structs are the same in C++ except that a class has everything private by default and a struct everything public, classes also inherit private by default where a struct inherit''s public.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
SnowKing3k
SnowKing3k
OK....
Everyone know that Class and Struct in C++ are exactly the same except the private and public thing. But this is not why you should use a Class instead of a Struct. A Class and a Struct must be use for respective reason, a Struct is only use to regroup some variables that have the same type ie

Struct Client
{
firstName;
lastName;
address;
telNumber;
age;
.......
};

This is the way u should use Struct on the other hand Class are use to regroup function ie

class Circle
{
public:
// Constructor
Circle::Circle()
{}
// Destructor
~Circle::Circle()
{}

// Other member function to do your circle.

private:
// Here you put your member variable that you will use to do
// your circle.
};

That's all folks.

-------------------------
Knowledge Is For Everyone

SnowKing3k
SnowKing3k@hotmail.com

[edited by - SnowKing3k on March 1, 2003 10:16:27 AM]
-------------------------Knowledge Is For EveryoneSnowKing3kSnowKing3k@hotmail.com
civguy
civguy
Some errors, SnowKing3k:
quote:

Struct Client
{
firstName;
lastName;
address;
telNumber;
age;
.......
};
First, "Struct" should be lowercase. Second, you should define types for the members (std::string firstName; int age; etc.)
quote:
Original post by SnowKing3k
Struct is only use to regroup some variables that have the same type ie
This is also not true.
quote:

class Circle
{
public:
Circle::Circle(){}
~Circle::Circle(){}
[...]
};

That's not how you make 'em. Circle:: is used only when the member is defined outside the class declaration. That code should be:

class Circle
{
public:
Circle(){}
~Circle(){}
[...]
};

Or actually, if the constructor/destructor do nothing, you don't have to define them.

[edited by - civguy on March 1, 2003 11:03:19 AM]
SnowKing3k
SnowKing3k
I''m sorry for my little standards errors, it''s becauseit''s been a long i did this kind of declaration and definition. Sorry

But the essential is there and it''s true...

-------------------------
Knowledge Is For Everyone

SnowKing3k
SnowKing3k@hotmail.com
-------------------------Knowledge Is For EveryoneSnowKing3kSnowKing3k@hotmail.com
Alpha_ProgDes
Alpha_ProgDes
i think a better (even if it''s a little better) question is whether a program with a C struct(s) will run faster than a program with C++ struct(s)/public class(es).

i am assuming that you are using a C compiler and a C++ compiler and comparing the results.
Beginner in Game Development?  Read here. And read here.  
SnowKing3k
SnowKing3k
Your right AP.....
What I said is not totally true you can use class to do the same thing as a struct but the thing is that you should not do it cause it''s not standard class and struct have been made for specific use... ie if you build a struct circle with some function like ChangeSize(), Rotate(), Fill(), ChangeColor(),... your wrong cause u should use class for this it''s just by principle. Another ie if u build a class that only contain data and u don''t any manipulation on it in this class u should use a struct. I''m only talking about standard here nothing in C++ prevent of doing that.

-------------------------
Knowledge Is For Everyone

SnowKing3k
SnowKing3k@hotmail.com
-------------------------Knowledge Is For EveryoneSnowKing3kSnowKing3k@hotmail.com
Qw3r7yU10p!
Qw3r7yU10p!
In C structures are a way of grouping data together.

In C++ Bjarne wanted something that would be able to be used for encapsulation, polymorphism, object orientation. He created the class keyword but structures are also classes as are unions. Structures remain backwards compatible with C structures in their syntax. However by using new keywords in C++ you are able to treat them as classes with accessing rights, give them member functions, inherit from them etc.

There is no restriction on how you should use classes or structures as they are equally capable.

If anyone says otherwise they are making it up.
Martee
Martee
quote:
Original post by Anonymous Poster
Dont forget that in terms of memory, structs are stack allocated and classes are allocated on the heap.

What?
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
SabreMan
SabreMan
quote:
Original post by Anonymous Poster
Dont forget that in terms of memory, structs are stack allocated and classes are allocated on the heap.

That''s a pretty stupid assertion, even for Gamedev.
NotAnAnonymousPoster
NotAnAnonymousPoster
quote:
Original post by Anonymous Poster
Dont forget that in terms of memory, structs are stack allocated and classes are allocated on the heap. Stack allocation is faster that dynamic heap allocation.

I think he''s thinking of C#.
"C combines all the power of assembly language with all the ease of use of assembly language"
SabreMan
SabreMan
quote:
Original post by Anonymous Poster
struct == record

In C, that''s correct. In C++, struct is a synonym for "class", meaning it is more than just a record.
quote:

class == object

A class is not the same thing as an object.
Fruny
Fruny
quote:
Original post by SabreMan
A class is not the same thing as an object.


Unfortunately.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
CGameProgrammer
CGameProgrammer
If we''re going to bicker about good code design, then the way I see it, there are three good ways of deciding between struct/class:


  • Use classes for everything.
  • Use classes for anything with member functions. (or, you can allow structs to have constructor/destructors but not anything else)
  • Use classes for anything that needs private/protected data.


~CGameProgrammer( );
DevImg.net - Post screenshots, comment on others.
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
billybob
billybob
simplest way to decide: if you have to type public: as the first thing in the class, use a struct

but thats stupid, i use structs for anything without functions other than a constructor/destructor
Qw3r7yU10p!
Qw3r7yU10p!
In C++ it doesn't matter whether you use classes or structures. They both have the same functionality, you're not losing or gaining anything at runtime by using one over another. Nothing ! Please don't say the opposite to this without justifying your incorrect reply.

The only problem I've found is when I have to forward declare a class. Using the microsoft c++ compiler you have to get the keyword correct or else you get a warning. So if something is initially declared as a class you can't forward declare it as a struct . This means I tend to use class most of the time so I don't have to keep track of what is a class and what is a struct. For function objects I use struct cos I don't usually have to forward declare them.

[edited by - petewood on March 3, 2003 10:37:37 AM]
Rick Scott
Rick Scott
petewood is correct. There is no difference in C++ that matters.

The only differences are those that *you* attach. For instance, I use ''struct'' to mean Plain Old Data, basically the same way struct was used in C. Its a conceptual difference that can make things a tiny little bit easier if adhered to consistently.

But any special meaning you give to a class or a struct must be kept in perspective: *you* gave it that meaning, and if its not obvious, that meaning ought to be documented clearly in the code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.