虚函数问题 [英] Virtual function problem

查看:62
本文介绍了虚函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基类中有一个虚函数,然后在公共派生类中用同名的

函数覆盖。当我使用指向派生类的指针(ClassB * b; b-> func();)调用

函数时,将调用

基类函数派生的

类中的新函数。所有其他类似的函数(基类中的虚函数和

在派生类中被覆盖)工作正常,它只是这一个

函数。有什么问题?

解决方案

Kostatus写道:

我在基类中有一个虚函数,然后被覆盖


你无法移植覆盖一个函数。无法保证

功能代码存在于可修改的内存中。

由公共派生类中的同名函数组成。


哦,你的意思是*被覆盖*。嗯,那是完全不同的。当然,

该函数必须满足除了具有相同名称之外的要求。它必须

也有相同的参数和相同的返回类型(除了协变返回类型的

特例)。

当我使用指向派生类的指针调用
函数(ClassB * b; b-> func();)


如果你这样做,你调用未定义的行为并且很可能会使你的程序崩溃。在给定值的情况下,您可能无法取消引用未指定的指针。

调用
基类函数而不是派生<中的新函数课程。所有其他类似的功能(基类中的虚拟和
覆盖


覆盖
派生类中的
)工作正常,它是'只是这一个功能。有什么问题?




合乎逻辑的结论是你的代码中有一个错误。这个错误发生的最可能的地方是第42行,但你也应该

查看第37行以防万一。


换句话说,我们不知道没看到代码。

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


-Kevin

-

我的电子邮件地址有效,但会定期更改。

请联系我使用最近发布的地址。




" Kevin Goodsell"在消息中写道

我在基类中有一个虚函数,然后被覆盖
你不能覆盖一个函数。无法保证
功能代码存在于可修改的内存中。

由公共派生类中的同名函数。



哦,你的意思是*被覆盖*。嗯,那是完全不同的。当然,除了具有相同名称外,该功能必须满足要求。它必须具有相同的参数和相同的返回类型(协变返回类型的特殊情况除外)。




我检查过这些,它们都是相同的。

当我使用指向派生类的指针调用
函数时(ClassB * b; b-> func();)



如果你这样做,你会调用未定义的行为,并可能会使你的程序崩溃。你可能不会取消引用一个没有给出值的指针。




给它一个值,毫无疑问。它没有崩溃,但

调用了错误的功能。


ClassA

{

public :

virtual void func1();

virtual void func2();

};


ClassB

{

public:

void func1();

void func2();

};


void some_other_func()

{

ClassB * b;

b = new ClassB(some_arguments);

b-> func1(); //调用ClassB :: func1()

b-> func2(); //因某些奇怪的原因调用ClassA :: func2()

}


基类函数被调用而不是派生的
类中的新函数。所有其他类似的功能(基类中的虚拟和
覆盖



覆盖




eh ..无论如何。 ..

在派生类中)工作正常,它只是这一个功能。有什么问题?



换句话说,我们不知道没看到代码。

http://www.parashift.com/c++- faq-lit ... t.html#faq-5.8

-Kevin
-
我的电子邮件地址有效,但会定期更改。
要联系我,请使用最近发布的地址。



Kostatus写道:


我检查了这些,他们都完全一样。


我恐怕我不能接受你的话。

它有一个价值,毫无疑问。它没有崩溃,但是
调用错误的函数。


如果没有给出正确的值,崩溃不是必需的。在

中,调用错误的函数是一个完全可以接受的结果。

ClassA
{
public:
virtual void func1() ;
虚拟空洞func2();
};

ClassB
{
公开:
void func1();
void func2();
};

void some_other_func()
{ClassB * b;
b =新的ClassB(some_arguments);
b-> func1(); //调用ClassB :: func1()
b-> func2(); //因某些奇怪的原因调用ClassA :: func2()




对不起,我不相信你。主要是因为ClassA和ClassB之间没有关系




正如我之前建议的那样,发布代码。真正的代码。一个最小的,b $ b可编辑的例子来证明这个问题。


这里又是链接,你显然上次没心烦读它:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class (ClassB* b; b->func(); ) the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten in the the derived class) work fine, it''s just this one
function. What''s the problem?

解决方案

Kostatus wrote:

I have a virtual function in a base class, which is then overwritten
You cannot portably overwrite a function. There is no guarantee that
function code exists in modifiable memory.
by a
function of the same name in a publically derived class.
Oh, you mean *overridden*. Well, that''s completely different. Of course,
the function must meet requirements beyond having the same name. It must
also have the same arguments and the same return type (except for the
special case of covariant return types).
When I call the
function using a pointer to the derived class (ClassB* b; b->func(); )
If you do it that way you invoke undefined behavior and will probably
crash your program. You may not dereference a pointer that has not been
given a value.
the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten
overridden
in the the derived class) work fine, it''s just this one
function. What''s the problem?



The logical conclusion is that your code has a bug in it. The most
likely place for this bug to occur is line 42, though you should also
check line 37 just in case.

In other words, we have no idea without seeing the code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.



"Kevin Goodsell" wrote in message

I have a virtual function in a base class, which is then overwritten
You cannot portably overwrite a function. There is no guarantee that
function code exists in modifiable memory.

by a
function of the same name in a publically derived class.



Oh, you mean *overridden*. Well, that''s completely different. Of course,
the function must meet requirements beyond having the same name. It must
also have the same arguments and the same return type (except for the
special case of covariant return types).



I checked these, they''re all identical.

When I call the
function using a pointer to the derived class (ClassB* b; b->func(); )



If you do it that way you invoke undefined behavior and will probably
crash your program. You may not dereference a pointer that has not been
given a value.



It IS given a value, there''s no doubt about that. It does not crash, but
calls the wrong function.

ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()
}

the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten



overridden



eh.. whatever...

in the the derived class) work fine, it''s just this one
function. What''s the problem?


In other words, we have no idea without seeing the code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.



Kostatus wrote:


I checked these, they''re all identical.
I''m afraid I can''t take your word for this.

It IS given a value, there''s no doubt about that. It does not crash, but
calls the wrong function.
If it isn''t given a correct value, crashing isn''t a requirement. In
fact, calling the wrong function is a perfectly acceptable consequence.

ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()



I''m sorry, I don''t believe you. Mostly because there is no relationship
between ClassA and ClassB.

As I suggested before, post the code. The real code. A minimum,
compilable example that demonstrates the problem.

Here''s the link again, you apparently didn''t bother to read it last time:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于虚函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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