Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I kill a kitten each time I use struct everywhere instead of class?

Tags:

c++

class

struct

struct is public by default while class is private by default.

Lets take Ogre3D for example; if I change all class occurences with struct, it compiles (I guess), and the engine works just as before.

If I'm right, the compiled code is exactly the same as before, because it's only the compiler that does check if a private/protected methods are called, it's not checked at runtime.

If I'm still right, class is just a keyword that just makes its cute eyes and begging "please encapsulate your data: you'll save a kitten", while private/protected scopes are still up to the user.

I know I sound kinda lame or irrelevantly rebel (something like "C is KISS dude, don't go "

Back to the question: what does the standard say about this little difference between struct and class while generating machine code ? Why add a keyword and try to impress programmers with the so called "OO model" while it's totally not enforced then ? Was it influenced by java ?

like image 596
jokoon Avatar asked Jan 24 '11 17:01

jokoon


2 Answers

The standard says nothing about generating machine code at all.

struct was retained to make migrating legacy C code easier. Typically, C++ programmers use it for POD-like structures.

like image 107
Oliver Charlesworth Avatar answered Oct 14 '22 21:10

Oliver Charlesworth


Actually, class and struct are both checked at compile time. The only difference is whether the default for members who you've not explicitly specified the access for are public (for a struct) or private (for a class). Otherwise, they produce exactly the same objects. If you specify all of your access control explicitly, you can use either one and they will be the same.

like image 30
Jeremiah Willcock Avatar answered Oct 14 '22 20:10

Jeremiah Willcock