簡體   English   中英

cpp/c 中的 const 用法和結構構造函數中的說明

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM