If I have a class, MyClass, can I declare an array of MyClass on the heap, within the constructor of MyClass?
I can do this in C# but it doesnt seem to like it in C++?
I also get an error about no appropriate constructor for type MyClass??
class MyClass
{
public:
MyClass(int);
private
MyClass* array;
};
MyClass::MyClass(int size){
array = new MyClass[size];
}
In order to have an array of something, said something has to be default-constructible. Your
MyClassisn’t since it needs anintto be constructed.What C# does is comparable to:
Where pointers are default constructible, so its allowed. Basically, whenever you do
SomeObjectin C# its the equivalent ofSomeObject*in C++. Except that the code would be horribly inefficient, even worse than its C# counterpart, and there would be leaks everywhere.