Struct vs class
by Chris French · in Technical Issues · 10/26/2005 (9:36 am) · 12 replies
Are there benefits to using a struct rather than a class? Do they have seperate uses?
My guess: More compact if you want to create a container for a few variables.
Any insight on the 2 would be most appretiated.
My guess: More compact if you want to create a container for a few variables.
Any insight on the 2 would be most appretiated.
#2
That being said here is a Quote from C/C++ creator
"By definition, a struct is a class in which members are
by default public" -- Stroustrup, p234
10/26/2005 (10:26 am)
I thought Structs do not have methods while Classes do.That being said here is a Quote from C/C++ creator
"By definition, a struct is a class in which members are
by default public" -- Stroustrup, p234
#3
structs use public variables by default, so to set a variable in a struct object would look like this.
To do the same thing with a class you would need a method (or member function - I believe) to set the variable, if the class is writting using conventions.
correct me if im wrong.
10/26/2005 (10:46 am)
Correct.structs use public variables by default, so to set a variable in a struct object would look like this.
StructureName structObject; // a new object made from structure structObject.objectsVariable = "whatever";
To do the same thing with a class you would need a method (or member function - I believe) to set the variable, if the class is writting using conventions.
ClassName classObject; // a new object made from a class
classObject.setObjectsVariable("whatever");correct me if im wrong.
#4
class myClass
{
int var1;
public:
int var2;
void setVar1(int value);
};
var1 can only be modified with the setVar1 function, but var2 can be modified directly.
10/26/2005 (10:56 am)
Well, its not wrong, if you want to set the (by default) private value you should use such utility functions, but with "public:" modifier you can avoid thisclass myClass
{
int var1;
public:
int var2;
void setVar1(int value);
};
var1 can only be modified with the setVar1 function, but var2 can be modified directly.
#5
10/26/2005 (11:01 am)
Anthony, in C++ structs can have methods too. its a little known fact but its true. the main difference is the public vs private setting for each.
#6
10/26/2005 (11:10 am)
So in most cases its just personal preference on which to use?
#7
10/26/2005 (12:25 pm)
There are more differences. for instance you can derive classes from other classes and generate a hierarchy of objects. i'm not sure, but i dont think the constructor, destructor, operator overloading, nor polymorphic functionality can be realized using structs. i might be wrong though. think of classes as an extension of structs. in general, you should get used to using classes instead of structs. it may not make a difference right now, but more you learn the more you're likely to come across something which structs cant do.
#8
10/26/2005 (12:40 pm)
Quote:There are more differences. for instance you can derive classes from other classes and generate a hierarchy of objects. i'm not sure, but i dont think the constructor, destructor, operator overloading, nor polymorphic functionality can be realized using structsSorry, Sean - but you can do all those things with structs also. The only difference is public vs. private.
#9
can structs have private properties and members as well?
what about merging structs and classes through multiple inheritance? or how about deriving a struct from a class and vice versa?
10/26/2005 (12:46 pm)
Really? thats amazingly stupid!can structs have private properties and members as well?
what about merging structs and classes through multiple inheritance? or how about deriving a struct from a class and vice versa?
#10
IMHO, C++ sucks ! I'm from a straight C generation that loved it for its "pureness". I agree with a Dr.Dobbs cover in the nineties that read "If C++ is the answer, what is the question ?".
As a mac user, I recently studied Objective-C. Now that's an interesting OOP approach. But it makes you write more code than C++.
10/26/2005 (6:20 pm)
Yes they can have private properties and everything else. I wouldn't know about merging structs and classes.IMHO, C++ sucks ! I'm from a straight C generation that loved it for its "pureness". I agree with a Dr.Dobbs cover in the nineties that read "If C++ is the answer, what is the question ?".
As a mac user, I recently studied Objective-C. Now that's an interesting OOP approach. But it makes you write more code than C++.
#11
10/27/2005 (6:57 am)
There is a second difference between classes and structs: Classes inherit privately by default, while structs inherit publically.
#12
I now feel confident in believing the 2 are, for the most part, the same thing, and I could get away with using either.
10/27/2005 (10:00 am)
Thanks for the input everybody. I now feel confident in believing the 2 are, for the most part, the same thing, and I could get away with using either.
Torque Owner Roland Stralberg
Rimfrost Software
struct has public members as default
class has private members as default.