gpt4 book ai didi

c++ - 如何在 C++ 的一条语句中设置多个类成员?

转载 作者:行者123 更新时间:2023-11-28 00:01:00 25 4
gpt4 key购买 nike

是否可以在一条语句中设置多个不同的类成员?只是一个如何做到这一点的例子:

class Animal
{
public:
int x;
int y;
int z;
};

void main()
{
Animal anml;

anml = { x = 5, y = 10, z = 15 };
}

最佳答案

要将 Barry 的评论“转换”为答案,是的,在 the conditions here 下:

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

例子:

class Animal
{
public:
int x;
int y;
int z;
};

int main() {
Animal anml;

anml = { 5, 10, 15 };
return 0;
}

(此 Community Wiki 答案是根据 this meta post 添加的。)

关于c++ - 如何在 C++ 的一条语句中设置多个类成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38927360/

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