c++ - 将结构与方法与函数和类一起使用之间的区别?
问题描述
最近我看到一个用 C++ 编写的 github 项目,它用structs
.
例子:
struct Example
{
std::string message;
Example()
{
message = "foo";
}
void setMessage(std::string newMsg)
{
message = newMsg;
}
void getMessage(std::string newMsg)
{
return message;
}
}
struct
在研究中,我发现定义对象和定义对象的区别在于class
:在中struct
,方法、函数和属性默认是公共的,class
同样默认是私有的。
我的问题是:使用结构比使用类有什么好处?
解决方案
推荐阅读
- sql - 纠正 sql 查询以在 2 分钟内排除会话
- javascript - ChartJS v2.9.4 - 通过图例隐藏数据集时可能出现的故障
- python - 为什么 keras 的准确性和损失在不同时期之间没有变化以及如何修复
- r - 在循环中添加 xts 对象
- c# - 已采取所有已知步骤,但 TLS 1.2 仍无法在 Windows 7 上运行
- java - jvm错误的算术答案
- mysql - MySQL 存储过程返回空结果集
- css - CSS,REACT:不能将兄弟姐妹放在一行上;
- python - 如何在python中找到哪个文件有keyerror?
- reactjs - res.data 在 json 数组的末尾向我发送了一个对象