开发者

Can the attributes of a class be an array?

I'm new to OOP, 开发者_如何学运维so please bear with me if this is a simple question. If I create a class, which has attributes "a", "b", and "c", is it possible for the attributes to be an array, such that attribute a[2] has a meaning?


Member variables can ofcourse be arrays. Example:

class MyClass {
    int a[3];  // Array containing three ints
    int b;
    int c;
};


Assuming that by "attributes" you mean what C++ refers to as "member variables" (i.e. members of a particular objects):

class MyClass:
public:
    MyClass() {
       a.push_back(3);
       a.push_back(4);
       a.push_back(5);
       cout << a[2] << endl; // should output "5"
    }
private:
    std::vector<int> a;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜