When should you use a class vs a struct in C++?
2015-03-26 16:18
281 查看
Question:
In what scenarios is it better to use a struct vs a class in C++?
Answer:
The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance,
and can have member functions.
I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.
In what scenarios is it better to use a struct vs a class in C++?
Answer:
The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance,
and can have member functions.
I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.
相关文章推荐
- Why should you choose fstream classes for file I/O in C++?
- Which variables should I typecast when doing math operations in C/C++?
- some issues that you should be take care of when use the plupload module
- [C++/Python] 如何在C++中使用一个Python类? (Use Python-defined class in C++)
- When should you store serialized objects in the database?
- When do we use Initializer List in C++?
- C++ -- struct vs class
- When should we write our own assignment operator in C++?
- When you should use base64 for images
- Set class as public when you add a new class using VS
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
- When to use static method in a java class
- Struct vs Class in .Net Framework
- When should I use Thread.getContextClassLoader()?
- PDO vs. MySQLi 选择哪一个?(PDO vs. MySQLi: Which Should You Use?)
- 使用PDO还是MySQLi(PDO vs. MySQLi: Which Should You Use?)
- Something about static Methods in C++ you should know
- Something you should know in C++
- Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.
- CodeIgniter 错误: In order to use the Session class you are required to set an encryption key