Jump to content
  • Advertisement

Archived

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

Oscar A

C++ struct and class

This topic is 5574 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 have a question about struct contra class in C++. What i know struct and class almost have the same syntax. My question is what is the differens and when should struct be better then a class. Maybe it is like C# where struct is a value type and you could pass it to a method as a value but a class is a reference typ and you could only pass it to a method as a reference. But in C++ you could pass a class by value to ??? The struct struct Hello { char msg[25]; Hello(char *pMsg) { if(pMsg != NULL) strcpy(msg, pMsg); } // Method void setName(char *pMsg) { if(pMsg != NULL) strcpy(msg, pMsg); } }; The class class Hello { private: char msg[25]; public: Hello(char *pMsg) { strcpy(msg, pMsg); } // Method void setName(char *pMsg) { if(pMsg != NULL) strcpy(msg, pMsg); } }; A would be happy for a answer from some expert out in the cyberspace :-) Happy Hacking. Don't forget to eat and sleep [edited by - Oscar A on March 17, 2003 4:34:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
in c++, the ONLY difference between struct and class is the default access modifier. For struct it is public, for class it is private.

Share this post


Link to post
Share on other sites
Some purists may advise using only classes. However, I often use a struct to group related data together (given that it will need no member functions).


pan narrans | My Website | Study + Hard Work + Loud Profanity = Good Code

Share this post


Link to post
Share on other sites
What sjelkjd said, with the addition that structs default to public inheritance and classes to private inheritance.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!