Classes/Structures
I''m having a hard time figuring out when to use a structure and when to use a class. For what I know a class can hold Variables and Functions while a structure is only a grouping of variables. So if I was making a game would I make PLAYER a structure or class? What are some other differences between them?
That''s a common misconception about C++. Classes and structs are the same thing. Both can be used in the same way.
The *only* differences between a class and a struct is that a class will have private as the default access modifier and will default to private inheritance, while a struct defauls to public as the access modifier and public inheritance.
The *only* differences between a class and a struct is that a class will have private as the default access modifier and will default to private inheritance, while a struct defauls to public as the access modifier and public inheritance.
Pretty much what was stated above but I''ll try and elaborate a bit more. With a structure all you have is the collection of variables like you said, which is indeed very handy. With a class you can have that same collection of variables (with or without accessor functions) and a collection of functions that will do some repetitive things for the class on its own. It''s all what you are trying to do. If you just need somewhere to store some simple variables then a structure will do fine, but if you need more functionality then classes are the way to go. They take a little while to get used to but are very helpful.
I know only that which I know, but I do not know what I know.
I know only that which I know, but I do not know what I know.
I know only that which I know, but I do not know what I know.
Daishi, that is incorrect. Reread my post above. A struct is a class. That means that anything you can do with a ''class'' you can also do with a ''struct''. There is no difference in functionality. The only differences are those I mentioned in my last post.
You can even declare a type as 'class' and then refine it as 'struct' (and conversely). Some idiot compilers will give a warning, but it is perfectly valid.
Daishi: C++ programmers call what you thought was a struct a 'POD' for 'Plain Old Data'.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 30, 2002 7:24:04 PM]
class Foo;...struct Foo{ int a;};
Daishi: C++ programmers call what you thought was a struct a 'POD' for 'Plain Old Data'.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 30, 2002 7:24:04 PM]
"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
June 30, 2002 07:15 PM
there is no difference other than the public/private thing. So use which you prefer. I prefer the sound of "class" but struct ends up being less typing so that''s what I use. Think about it, some types have just public, some have public and private, while you''ll just about never have a type that has nothing public.
even if classes and structs did differ, there would NEVER a be reason to make a struct (besides creating code to work with WIN32 API and other legacy). the additional functionality provided by a class, with constructors and methods, will always make them more useful than a simple struct.
July 01, 2002 11:51 AM
EvilCrap: you are 100% incorrect. Classes simply do not offer additional functionality over structs. Everything a class can do a struct can do too. Stop posting info that is just plain wrong. You could have read Dactylos''s post before posting. If you don''t believe us throw some code at your compiler.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement