CPP/C 中的常量用法和结构构造函数中的澄清
const in cpp/c usage and clarification in struct constructor
我想澄清一下
struct Sphere
{
int a;
Sphere() {}
Sphere(int given)
{
a = given;
}
Sphere (const Sphere &s)
{
a=s.a;
}
};
当我这样做时:
Sphere mySphere;
mySphere.a = 5;
调用哪个构造函数?const
在这里的作用是什么?如果我省略const
构造函数,则不会分配 a
的值。为什么?
调用哪个构造函数?
默认构造函数。
常量在这里的作用是什么?
都不是。
如果我省略 const 构造函数,则不会分配 a 的值。为什么?
我不知道,这没有意义。
您没有使用复制构造函数。完全。
您所做的只是默认构造一个Sphere
然后分配给其数据成员。
相关文章:
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 嵌套的模板结构构造函数实现
- 其他结构中的结构构造函数
- CPP/C 中的常量用法和结构构造函数中的澄清
- 聚合结构构造函数
- 结构构造函数调用不明确
- 关于结构构造函数和析构函数行为-C++
- 带有指针的C++结构:构造函数和析构函数
- 结构构造函数导致堆栈溢出的问题
- C++结构构造函数和函数冲突
- 嵌套结构构造函数和并集的问题
- C++结构构造函数
- 调用结构构造函数后初始化的值是什么
- 在结构构造函数中使用"this"关键字编译错误?-C++
- 这可能吗?在 C# 中调用托管 c++ 结构构造函数
- C++ - 结构构造函数,尝试取消指向结构的指针时出现重定义错误
- C++结构构造函数错误
- 结构构造函数语法
- 结构构造函数错误,为什么使用默认构造函数会调用错误
- Qt 将 QString 传递给结构构造函数不起作用