通过指向该类的抽象父类的指针执行该类的成员函数

发布于 2024-09-10 16:50:11 字数 1242 浏览 9 评论 0原文

我创建了一个抽象基类 Animal,它具有公共虚拟抽象方法 makeSound()。我创建了一个子类 Cow ,它按照您的预期实现了 Animal.makeSound() (您知道...“moo”)。我有一个 Farm 类,它包含一个私有成员变量 std::vector;动物。在其中一种 Farm 方法中,我迭代所有动物并让它们发出声音。

for(unsigned int i = 0; i < animals.size(); i++)
{
   animals[i]->makeSound()
}

不幸的是我收到一个错误

在 0x65766974 处出现未处理的异常 TestBed.exe:0xC0000005:访问 违规读取位置 0x65766974。

知道这是怎么回事吗?


更新:为每个请求添加更多代码

class Farm
{
public:
    Farm();
    virtual ~Farm(void);

    void setBarnOnFire();

private:
    vector<Animal*> animals;
};


Farm::Farm()
{
    animals.push_back(new Dog());
    animals.push_back(new Cat());
    animals.push_back(new Chicken());
    animals.push_back(new Horse());
    animals.push_back(new Cow());
}



Farm::setBarnOnFire()
{
    for(unsigned int i = 0; i < animals.size(); i++)
    {
       animals[i]->makeSound()
    }
}

我应该做些什么来初始化动物

结论:

所以你们都是对的。我正在访问不属于我的内存。但我花了很长时间才找到它。这是由于对对象初始化如何发生的误解。基本上,为了“初始化”成员变量,我实际上是用局部变量覆盖它。然后我给我创造的所有动物赋予了本地化。后来,动物会尝试调用不再存在的局部变量。

I have created an abstract base class Animal which has public virtual abstract method makeSound(). I created a subclass Cow which implements Animal.makeSound() as you would expect (you know... "moo"). And I have a Farm class which holds a private member variable std::vector<Animal*> animals. In one of the Farm methods I iterate over all animals and make them make their sound.

for(unsigned int i = 0; i < animals.size(); i++)
{
   animals[i]->makeSound()
}

Unfortunately I get an error

Unhandled exception at 0x65766974 in
TestBed.exe: 0xC0000005: Access
violation reading location 0x65766974.

Any idea what's going on here?


UPDATE: adding more code per request

class Farm
{
public:
    Farm();
    virtual ~Farm(void);

    void setBarnOnFire();

private:
    vector<Animal*> animals;
};


Farm::Farm()
{
    animals.push_back(new Dog());
    animals.push_back(new Cat());
    animals.push_back(new Chicken());
    animals.push_back(new Horse());
    animals.push_back(new Cow());
}



Farm::setBarnOnFire()
{
    for(unsigned int i = 0; i < animals.size(); i++)
    {
       animals[i]->makeSound()
    }
}

Is there something I'm supposed to do to initialize animals.

RESOLUTION:

So you were all correct. I was accessing memory that I didn't own. But it took me forever to track it down. It was due to a misunderstanding about how object initialization takes place. Basically, in an effort to "initialize" a member variable I was actually overwriting it with a local variable. I then gave the local to all the animals that I created. Later, the animals would try to call the local variable - which no longer existed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-09-17 16:50:11

好吧,让我猜测一下:

“TestBed.exe 中 0x65766974 处出现未处理的异常:0xC0000005:读取位置 0x65766974 时发生访问冲突。”

看起来代码指针被发送到0x65766974(“0x65766974处的异常”),但这不是一个有效的读取位置,更不用说代码了:(“访问冲突读取位置0x65766974”,注意,相同的数字)

所以是vtable 或 vtable 指针是否可能已损坏?
也许该对象正在被字符串覆盖?当它存储在向量中时,也许向量中前一个对象中的缓冲区(可能是字符数组?)溢出,这会破坏下一个对象的虚函数表指针?

ok, let me take a guess:

"Unhandled exception at 0x65766974 in TestBed.exe: 0xC0000005: Access violation reading location 0x65766974."

it seems that the code pointer is being sent to 0x65766974 ("exception at 0x65766974") but this is not a valid place to be reading, let alone code: ("Access violation reading location 0x65766974", note, the same number)

so is it possible the vtable, or vtable pointer is being corrupted?
perhaps the object is being overwritten by a string? as it is being stored in a vector, perhaps you have something overflowing a buffer (maybe a char array?) in the preceding object in the vector, and this is corrupting the next objects vtable pointer?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文