如何在 C++ 中访问向量 class 数组方法

How to access vector class array methods in c++

class Bird{

public:
    void init();
    Bird();//constructor
    void foo();//its defined somewhere
};


int _tmain(int argc, _TCHAR* argv[])
{
    std::vector <Bird> B[51];

    for (int i = 0; i < 51; i++)
       B[i].foo();
}

看起来 vector 是一种安全且现代的安全生成 51 对象的方法。

假设我从 class Bird 创建了一个对象数组。并想使用每个对象的方法。 (我不能使用静态对象数组,因为稍后我必须交换数组成员。)



(我从 2000 年开始就没有使用 C++。现在我不得不)

正如 Boo 先生的回应。

class Bird{

public:
    void init();
    Bird();//constructor
    void foo();//its defined somewhere
};


int _tmain(int argc, _TCHAR* argv[])
{
    std::vector <Bird> B(51); // fixed line

    for (int i = 0; i < 51; i++)
       B[i].foo();
}