What exactly is the difference between a struct and a class?
What are the advantages/disadvantages of one over the other?
Printable View
What exactly is the difference between a struct and a class?
What are the advantages/disadvantages of one over the other?
One has private members, one has public members. Read your book.
Quzah.
I am reading my book, and I found a code example with a struct with both private and public members...
And I already know a class can have private, protected, and public members, so what is the difference? Can a struct not have protected members, or derived structs?
Why would one use a struct instead of a class? Do they allocate different sizes of memory, maybe the class more than the struct?
You book should have already explained the difference. They are identical except for the default scope.
Quzah.
Right. I think it did say something about that, but I forgot it until you mentioned it.
(Btw...do you know what book I'm reading?)
So...the default scope for a class is public, and the default scope for a struct is private?
Ahh, ok, thanks.
Structs cannot have methods other than a constructor I believe....
Sorry, that is all I have ever seen in a struct....and it was just initializing members. I have seen structs with function pointers before but that was a syntactic mess.....so is your FAIL that they cannot have ctors or that they cannot have anything or they can have everything?
The ONLY difference is the default scope.
A struct can have anything a class can have.
I'll be, y'all are right. I have not seen structs used in such a fashion but GCC has no problem with it. Learn something new everyday...
^__^
To elaborate: a struct is a class for which bases and members have public access by default; a class is a struct for which bases and members have private access by default. Often a convention is adopted such that the struct keyword is reserved for defining POD structs or a simple aggregation of objects.