Reputation: 167
Okay, so I'm experiencing some very weird issues with storing an array of base class pointers in a class then settings this base class pointers equal to some dynamically allocated derived class pointers. My code is too long and bloated with GUI calls to post here, so I'll make some mock code here that demonstrates the methodology I'm using
Base Class - Base
class Base
{
public:
Base(){ // do nothing}
virtual void SetUI(){ // do nothing}
}
Derived Class - Derived
class Derived : public Base
{
public:
Derived() : Base() { // do nothing}
virtual void SetUI(){// do nothing}
}
This is a class that basically holds data for my program, it contains an array of pointers to the base class. These pointers are initialized by having them point to dynamically created objects of the derived class type.
class HelperClass
{
private:
Base * basearray[2];
public:
HelperClass()
{
basearray[0] = new Derived;
basearray[1] = new Derived;
}
Base * getBaseArray(int key)
{
return this->basearray[key];
}
}
here is my "main", this is where everything starts getting weird.
int main()
{
HelperClass hold;
hold.getBaseArray(0)->setUI();
// NOTHING HAPPENS ABOVE, THE CODE DOESN'T EVEN REACH THIS POINT, IT JUST GET'S LOST SOMEWHERE IN //THE ABOVE STATEMENT. i KNOW THIS BECAUSE I PUT AN exit(0) inside the function setUI() for both the //base and derived class and the program doesn't exit.
// this also does nothing
if(hold->getBaseArray(0) || !hold->getBaseArray(0))
exit(0);
return 0;
}
but oddly enough, if i do something like the following, where I just declare a base class pointer and have it point to a dynamically created object of the derived class, everything works just fine.
int main()
{
Base *hold;
hold = new Derived;
hold->setUI(); // <- this works polymorphically
return 0;
}
Does anyone have any idea why my program stops working when I try to work with the array of base class pointers inside the HelperClass? When I try to do getBaseArray(int) it doesn't return anything, which causes statements like
if(hold->getBaseArray(0) || !hold->getBaseArray(0)) {exit(0);}
to not make the program exit, which is weird because the pointer that getBaseArray() returns is either null or non-null, which means the program should exit no matter what
Upvotes: 1
Views: 168
Reputation: 482
I (and my compiler) found two mistackes you have in the first main()
SetUI()
in your Base and Derived class, but in the main
you try to call setUI()
. Perhaps you have both functions in your project and that's why you recieved nothing.hold
as an object and not a pointer. With this you can not do like hold->someFn()
. You should call thet like hold.someFn()
.Here is the code with corrections. It runs by me without problems. Try it.
struct Base
{
Base(){}
virtual ~Base() {}
virtual void SetUI() {
cout << "base" << endl;
}
};
struct Derived : public Base
{
Derived() : Base() {}
virtual ~Derived() {}
virtual void SetUI() {
cout << "derived" << endl;
}
};
class HelperClass
{
Base * basearray[2];
public:
HelperClass() {
basearray[0] = new Derived;
basearray[1] = new Derived;
}
~HelperClass() {
delete basearray[0];
delete basearray[1];
}
Base *getBaseArray(int key) {
return basearray[key];
}
};
int main()
{
HelperClass hold;
hold.getBaseArray(0)->SetUI();
if(hold.getBaseArray(0))
cout << "some output" << endl;
return 0;
}
Upvotes: 1