Archived

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

Oscar A

C++ struct and class

Recommended Posts

Oscar A    122
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
sjelkjd    171
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
pan narrans    1284
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