gpt4 book ai didi

C++ 方法声明

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:44 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Benefits of Initialization lists

过去几天我一直在学习 C++,我看到了两种格式,我无法确定这两种格式的优缺点。希望有人能在这里帮助我。

第一个变量使用 var(value)

初始化
class Foo
{
public:
Foo(): itsVar1(2), itsVar2(345){}

private:
int itsVar1;
int itsVar2;
};

第二个使用赋值运算符 var = value 进行初始化。

class Foo
{
public:
Foo()
{
itsVar1 = 2;
itsVar2 = 345;
}

private:
int itsVar1;
int itsVar2;
};

一个比另一个有优势吗?是个人喜好吗?
第一种风格(?)看起来更让我困惑。看起来您正在调用一个方法并传入该值。看起来很含蓄;然而,第二种方法非常明确,作为来自 Python 的人“明确优于隐含”,我更喜欢第二种方法。我错过了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com