涉及到指针的成员变量和多态性很奇怪的问题, [英] Very weird problems involving pointer member variables and polymorphism

查看:136
本文介绍了涉及到指针的成员变量和多态性很奇怪的问题,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我遇到一些很奇怪的问题,与存储基类指针数组中的一类,然后设置这个基类指针等于某个动态分配的派生类的指针。
我的code是太长,臃肿的GUI要求张贴在这里,所以我会做一些模拟$ C $这里C,它展示了我使用的方法

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

基类 - 基本

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;
}

有没有人有任何想法,为什么我的程序停止时,我尝试用助手类里面基类指针数组上班工作?当我尝试做getBaseArray(INT)它不返回任何东西,这会导致类似的语句

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);}

不使程序退出,这是奇怪的,因为这getBaseArray()返回指针为null或非空,这意味着程序应该退出不管是什么

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

推荐答案

我(和我的编译器),发现在第一个有两个mistackes 的main()

I (and my compiler) found two mistackes you have in the first main()


  1. 您声明 SetUI()在您的基类和派生类,但在你试图调用 setUI()。也许你在你的项目有两个功能,这就是为什么你收到没有。

  2. 您声明持有作为一个对象,而不是一个指针。有了这个,你不能做喜欢相应固定&GT; someFn()。你应该叫泰德像 hold.someFn()

  1. You declare 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.
  2. You declare hold as an object and not a pointer. With this you can not do like hold->someFn(). You should call thet like hold.someFn().

下面是code。与更正。它运行由我没有问题。试试吧。

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;
}

这篇关于涉及到指针的成员变量和多态性很奇怪的问题,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆