虚函数指针(& multiple inheritance?) [英] Virtual function pointers (& multiple inheritance?)

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

问题描述

大家好。我正在维护一个C ++程序,我遇到了一个令人讨厌的作品

代码可行,但我只是不明白为什么。我实际上并不是这个程序的一部分,但我真的想知道它是如何工作的原因。

我会张贴一个下面的简化版本并问我的问题

之后:


class Base {

void * function_ptr;


public:

Base();

void Initialize();

virtual void Function(); < br $>
};


Base :: Base(){

function_ptr =(void *)& Base :: Function; < br $>
}


void Base :: Initialize(){//应该从派生构造函数调用

if(function_ptr!=( void *)& Base :: Function){

/ *做点什么* /

}

}


void Base :: Function(){

/ * some code * /

}

class派生:public virtual Base {

public:

Derived();

virtual void Function();

};


派生::派生(){

Initialize(); < br $>
}


void Derived :: Function(){

/ * some code * /

}

因此派生类会覆盖虚函数Function。现在,当

派生类调用Initialize(非虚拟并在

基础中定义)时,if子句中的花括号之间的语句

实际上已被执行。为什么这样做?似乎基础是

将function_ptr设置为& Base :: Function,但是当它在Initialize()中再次检查

时,它们不再是相等。


这是否与Base是一个虚拟的
基类这一事实有关(即如果它不是' 'ta虚拟基地)?据我所知,至少

,虚拟基数仅用于防止多继承树中多个副本的

基类,但最近在此讨论了

新闻组让我想知道是不是(相当多)更多的东西

比这更好。


提前谢谢任何帮助。


********************************** **************** ***

Josh Lessard

硕士学生

计算机科学学院

数学系

滑铁卢大学

(519)888-4567 x3400
http://www.cs.uwaterloo.ca

**** ********************************************** ***

Hi all. I''m maintaining a C++ program and I''ve come across a nasty piece
of code that works, but I just don''t understand why. I''m not actually
this part of the program, but I really want to know how and why it works.

I''ll post a simplified version of it below and ask my questions
afterwards:

class Base {
void *function_ptr;

public:
Base();
void Initialize();
virtual void Function();
};

Base::Base() {
function_ptr = (void *)&Base::Function;
}

void Base::Initialize() { // should be called from derived constructor
if ( function_ptr != (void *)&Base::Function ) {
/* do something */
}
}

void Base::Function() {
/* some code */
}
class Derived : public virtual Base {
public:
Derived();
virtual void Function();
};

Derived::Derived() {
Initialize();
}

void Derived::Function() {
/* some code */
}
So the derived class overrides the virtual function "Function". Now, when
the derived class calls Initialize (which is non-virtual and defined in
the base), the statements between the curly braces in the if clause
actually get executed. Why does this work? It seems like the base is
setting function_ptr to &Base::Function, but then when it checks it again
in Initialize(), they''re no longer equal.

Does this have anything at all to do with the fact that Base is a virtual
base class (ie would this still work if it wasn''t a virtual base)? As far
as I knew, virtual bases are used only to prevent multiple copies of the
base class in a multiple inheritance tree, but recent discussion in this
newsgroup has caused me to wonder if there isn''t (quite a bit) more to it
than that.

Thanks in advance for any help.

************************************************** ***
Josh Lessard
Master''s Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
************************************************** ***

推荐答案



" Josh Lessard" < JR ****** @ plg2.math.uwaterloo.ca>在留言中写道

新闻:Pi ************************************ ** @ plg2 .math.uwaterloo.ca ...

"Josh Lessard" <jr******@plg2.math.uwaterloo.ca> wrote in message
news:Pi**************************************@plg2 .math.uwaterloo.ca...
function_ptr =(void *)& Base :: Function;
function_ptr = (void *)&Base::Function;




这不是合法的演员。不能保证会员函数指针可以被安全地使用

来无效*


你想做什么?



This isn''t a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.

What are you trying to do?


Josh Lessard写道:
Josh Lessard wrote:
...
我正在维护一个C ++程序而且我遇到了一个令人讨厌的部分 ...
...
I''m maintaining a C++ program and I''ve come across a nasty piece
of code that works, but I just don''t understand why. I''m not actually
this part of the program, but I really want to know how and why it works.
...




没有办法说出它为什么会起作用。您的代码已损坏(请参阅Ron的

回复),其行为未定义。它似乎是正在工作。如果

你很幸运,如果天气合适的话。明天它可能会开始

一直崩溃。


-

祝你好运,

Andrey Tarasevich



There''s no way to say why it works. Your code is broken (see Ron''s
reply) and its behavior is undefined. It might appear to be "working" if
you are lucky and if the weather is right. And tomorrow it might start
to crash consistently.

--
Best regards,
Andrey Tarasevich


Ron Natalie写道:
Ron Natalie wrote:
" Josh Lessard" < JR ****** @ plg2.math.uwaterloo.ca>在消息中写道
新闻:Pi ************************************** @ plg2 .math.uwaterloo.ca ...

"Josh Lessard" <jr******@plg2.math.uwaterloo.ca> wrote in message
news:Pi**************************************@plg2 .math.uwaterloo.ca...

function_ptr =(void *)& Base :: Function;
function_ptr = (void *)&Base::Function;



这不是合法的演员。不能保证成员函数指针可以安全地转换为void *。

你想做什么?


This isn''t a legitmate cast. No guarantee that a member function pointer can be cast
to void* safely.

What are you trying to do?




是不是很明显?


有人利用编译器错误确定是否在Base的构造函数中调用了
Initialize或

不是。白痴留下的评论说得很多。



Isn''t it obvious?

Someone has exploited a compiler bug to determine whether
Initialize is being called in the constructor of Base or
not. The comment the idiot left says as much.


这篇关于虚函数指针(&amp; multiple inheritance?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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