[英]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在這里的作用是什么?
沒有任何。
如果我省略 const 構造函數,則不會分配 a 的值。 這是為什么?
我不知道,這沒有意義。
您沒有使用復制構造函數。 在所有。
您所做的只是默認構造一個Sphere
然后分配給它的數據成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.