Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nexster

Classes/Structures

This topic is 5955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!