gpt4 book ai didi

c++ - 具有多个参数的对象数组

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

如何将类的两个参数切换到我正在创建的对象?语法是什么? (他们必须接收所有相同的参数)

#include <iostream>

using namespace std;

class MyClass
{
public:
MyClass(int x,int y):value(x),value2(y)
{
//nothing
}
int value=10;
int value2;
};

int main()
{
MyClass ob1[5]; //Here! What is the correct syntax?
cout << ob1[0].value << endl;
return 0;
}

最佳答案

你可以使用

MyClass ob1[] = {{1,2},{2,3},{3,4},{4,5},{5,6}};

(如果您使用的是 C++11 或更高版本)。

但是,最好使用 std::vector 或 std::array。

关于c++ - 具有多个参数的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43308540/

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