I am unsure how to create an array of objects within its own class. For example:
class A {
public:
const static int MAX_SIZE = 10;
private:
A arrayOfOBjects[MAX_SIZE];
}
I get an error saying, "incomplete type is now allowed" How would I go about this? If I declare an array of objects from another class inside Class A it will work.. But how do I create an array of objects within its own class?
use a pointer. or shared_ptr etc and dynamically allocate the objects
class A {
public:
const static int MAX_SIZE = 10;
private:
A * arrayOfOBjects[MAX_SIZE];
}
You obviously can't contain an instance of the object in itself. It would take infinite amount of storage ;-)
For the same reason you can't contain several. Maybe you meant the array to be a static member that doesn't add to the state of one instance?
By using pointers you can solve this problem
class A {
public:
const static int MAX_SIZE = 10;
void allocate(){
arrayOfOBjects=new A[MAX_SIZE];
}
private:
A *arrayOfOBjects;
};
You can use this as following
A a;
a.allocate();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.