C++中class和struct的区别
面试题很喜欢问的题目。找了一些资料,发现两者只有以下区别:
1. In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.
2. Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.
翻译过来就是:
1. 默认继承权限的区别:如果不明确指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理。
2. 成员默认访问权限的区别:class的成员默认是private权限,struct默认是public权限。
其它方面,例如各类构造函数,虚函数,重载预算符,复杂继承多继承什么的,两者都是一样。
参考:
http://blog.csdn.net/swanzy/article/details/3130331
http://www.justsoftwaresolutions.co.uk/cplusplus/struct_and_class.html