I need to allocate memory for objects inside another object. The child would be a private member of the parent, and the parent would have a constructor that recievs an int that is the number of needed objects to be allocated. I did it like this but I am getting a segmentation fault when I try to modify the child objects...
(ps how many ways are there for dynamic allocation?)
1 2 3 4 5 6 7 8
class Parent
{
private:
Child *children;
public:
Parent(n);//constructor
}
This is the constructor:
1 2 3 4
Parrent::Parent(int n)
{
children = new Child[n];
}
Then I have a method that calls children[i].con() (i can be any number, even just 0 ) and that is where I get the segfault... the con constructor works when I test the child, but like this in the parrent something goes wrong...
which works when I test the child class, but like this I get a segfault...
I cannot as this is an assignment... The thing is I missed a lecture about this and it is confusing... I will give you my professors example, and hope someone can explain it just a bit! What I need is someone to explain how I will allocate objects for a "Command" class. I understand the point of it all, but not how it is done. Why is thevoid memory(constchar*); inside the private sector? I understand the try, catch and so on. Its just the allocation I am confused with, especially because this is for a string and I need to allocate an array of Command objects!