来自对象的虚拟成员函数的地址(* not * class) [英] Address of virtual member function from object (*not* class)

查看:74
本文介绍了来自对象的虚拟成员函数的地址(* not * class)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


出于好奇:有没有办法在给定对象的情况下获取

特定虚拟成员函数的地址?一些代码使

的东西更加清晰...


A级

{

public :

A(){}

虚拟空虚f(){}

};


级B:公共A

{

公开:

B(){}

虚拟空虚f( ){}

};


typedef void(A :: * FPtr)();


FPtr getFAddress(A * obj)

{

return& obj-> f; // FIXME这不会编译(至少在VC ++ 8上)

}


int main()

{

A a;

B b;


getFAddress(& a); //应返回& A :: f

getFAddress(& b); //应返回& B :: f

}

intuituve这个getFAddress实现的解决方案

甚至不能在VC ++ 8上编译(如果重要的话,错误C2276)。注意

f是虚拟的,所以地址取决于对象的类。现在,

如果编译器可以生成代码来获取正确的地址

f成员,为什么我们不能将它用于我们自己的代码? (好吧,这可能导致很多灾难性的代码 - 比如多态的结束,因为我们知道它,但无论如何......)。我真的不认为有一个便携的

解决方案,但如果有人之前遇到过这个问题,我很高兴看到他们的解决方案。


谢谢,


Gabriel

Hi all!

Just out of curiosity: Is there any way to get the address of a
particular virtual member function given an object? Some code to make
things more clear...

class A
{
public:
A() { }
virtual void f() { }
};

class B: public A
{
public:
B() { }
virtual void f() { }
};

typedef void (A::*FPtr)();

FPtr getFAddress(A* obj)
{
return &obj->f; // FIXME This won''t compile (at least on VC++ 8)
}

int main()
{
A a;
B b;

getFAddress(&a); // Should return &A::f
getFAddress(&b); // Should return &B::f
}
The "intuituve" solution of this implementation of getFAddress
doesn''t even compile on VC++ 8 (error C2276 if that matters). Note
that f is virtual, so the address depends on the object''s class. Now,
if the compiler can generate the code to get the address of the right
f member, why can''t we use this for our own code? (OK, this may lead
to lots of catastrophic code -- like the end of polymorphism as we
know it, but anyway...). I don''t really think there is a portable
solution for this, but if someone has had this problem before, I''m
curious to see their solution.

Thanks,

Gabriel

推荐答案

?o ?星期二,2008年9月2日05:22:50 -0700 ??? Gabriel de Dietrich ?????°???
?o? Tue, 02 Sep 2008 05:22:50 -0700???Gabriel de Dietrich?????°???

大家好!


只是出于好奇:有没有办法在给定对象的情况下获取特定虚拟成员函数的地址?一些代码使

的东西更加清晰...


A级

{

public :

A(){}

虚拟空虚f(){}

};


级B:公共A

{

公开:

B(){}

虚拟空虚f( ){}

};


typedef void(A :: * FPtr)();


FPtr getFAddress(A * obj)

{

return& obj-> f; // FIXME这不会编译(至少在
Hi all!

Just out of curiosity: Is there any way to get the address of a
particular virtual member function given an object? Some code to make
things more clear...

class A
{
public:
A() { }
virtual void f() { }
};

class B: public A
{
public:
B() { }
virtual void f() { }
};

typedef void (A::*FPtr)();

FPtr getFAddress(A* obj)
{
return &obj->f; // FIXME This won''t compile (at least on



VC ++ 8)

VC++ 8)


}


int main()

{

A a;

B b;


getFAddress(& a); //应返回& A :: f getFAddress
}

int main()
{
A a;
B b;

getFAddress(&a); // Should return &A::f getFAddress



(& b); //应该是

(&b); // Should


return& B :: f

}


intuituve"这个getFAddress实现的解决方案

甚至不能在VC ++ 8上编译(如果重要的话,错误C2276)。请注意,
f是虚拟的,因此地址取决于对象的类。现在,如果

编译器可以生成代码来获取正确的f成员的地址,

为什么我们不能将它用于我们自己的代码? (好吧,这可能会导致很多

灾难性代码 - 就像我们所知道的多态性的结束一样,但无论如何都是

......)。我真的不认为有一个可移植的解决方案,

但如果有人之前遇到过这个问题,我很想看到他们的

解决方案。


谢谢,


Gabriel
return &B::f
}
The "intuituve" solution of this implementation of getFAddress
doesn''t even compile on VC++ 8 (error C2276 if that matters). Note that
f is virtual, so the address depends on the object''s class. Now, if the
compiler can generate the code to get the address of the right f member,
why can''t we use this for our own code? (OK, this may lead to lots of
catastrophic code -- like the end of polymorphism as we know it, but
anyway...). I don''t really think there is a portable solution for this,
but if someone has had this problem before, I''m curious to see their
solution.

Thanks,

Gabriel



以下标题适合您吗?

http://www.codeproject。 com / KB / cpp / Im ... pDelegate.aspx

-

cch @srdgame

Is follow title suitable for you?

http://www.codeproject.com/KB/cpp/Im...pDelegate.aspx
--
cch@srdgame


Gabriel de Dietrich写道:
Gabriel de Dietrich wrote:

出于好奇:有没有办法获得

特定虚拟成员的地址函数给出了一个对象?一些代码使

的东西更加清晰...


A级

{

public :

A(){}

虚拟空虚f(){}

};


级B:公共A

{

公开:

B(){}

虚拟空虚f( ){}

};


typedef void(A :: * FPtr)();


FPtr getFAddress(A * obj)

{

return& obj-> f; // FIXME这不会编译(至少在VC ++ 8上)

}


int main()

{

A a;

B b;


getFAddress(& a); //应返回& A :: f

getFAddress(& b); //应返回& B :: f

}


intuituve这个getFAddress实现的解决方案

甚至不能在VC ++ 8上编译(如果重要的话,错误C2276)。注意

f是虚拟的,所以地址取决于对象的类。现在,

如果编译器可以生成代码来获取正确的地址

f成员,为什么我们不能将它用于我们自己的代码? (好吧,这可能导致很多灾难性的代码 - 比如多态的结束,因为我们知道它,但无论如何......)。
Just out of curiosity: Is there any way to get the address of a
particular virtual member function given an object? Some code to make
things more clear...

class A
{
public:
A() { }
virtual void f() { }
};

class B: public A
{
public:
B() { }
virtual void f() { }
};

typedef void (A::*FPtr)();

FPtr getFAddress(A* obj)
{
return &obj->f; // FIXME This won''t compile (at least on VC++ 8)
}

int main()
{
A a;
B b;

getFAddress(&a); // Should return &A::f
getFAddress(&b); // Should return &B::f
}
The "intuituve" solution of this implementation of getFAddress
doesn''t even compile on VC++ 8 (error C2276 if that matters). Note
that f is virtual, so the address depends on the object''s class. Now,
if the compiler can generate the code to get the address of the right
f member, why can''t we use this for our own code? (OK, this may lead
to lots of catastrophic code -- like the end of polymorphism as we
know it, but anyway...).



您的问题的答案是因为我们不需要。有很多东西,编译器可以做这个程序本身不能做的事情。对于

示例,我非常肯定编译器类(我们的

程序中的UDT)是对象。在我们的程序中,它们不是......

The answer to your question is "because we don''t need to". There are
many things that compiler can do that the program itself can''t. For
example, I am quite certain in the compiler classes (the UDTs in our
programs) are objects. In our programs they are not...


我真的不认为有一个便携的

解决方案,但是如果有人之前遇到过这个问题,我很高兴看到他们的解决方案。
I don''t really think there is a portable
solution for this, but if someone has had this problem before, I''m
curious to see their solution.



好奇号我相信,这不是一个真正有效的理由。所以,真的,为什么

你想要那么做?你没有以任何方式在

中使用指针成员,那么返回它的重点是什么?


V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要不要问

"Curiosity" is not really a valid reason, I believe. So, really, why
would you want to do that? You''re not using the pointer-to-member in
any way, so what would be the point of returning it?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


9月2日下午3:28 *,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:
On Sep 2, 3:28*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

" Curiosity"我相信,这不是一个真正有效的理由。 *所以,真的,为什么

你想要那样做? *你没有以任何方式在

中使用指向成员的指针,那么返回它的重点是什么?
"Curiosity" is not really a valid reason, I believe. *So, really, why
would you want to do that? *You''re not using the pointer-to-member in
any way, so what would be the point of returning it?



嗯,这对我来说...... :-)


回答你的问题:假设你有任意的大的

继承层次结构与根类A,你想知道是否有一些

子类重新实现f。如果你知道班级,没问题。只需测试


& A :: f ==& B :: f


或其他任何类别,它是'完成。现在,假设你不知道这个类是什么,因为你拥有的是一个指向对象的多态指针

(就像在A * obj中一样)。在这种情况下你会测试


& A :: f == getFAddress(obj)


使用我的示例代码,这将是如果* obj的类是A,

则为true,如果它是B,则为false。此外,您不知道

继承层次结构中的所有类,所以你不能(或者不想)使用typeid,dynamic_cast或

检查所有类的

对象。


至于最终目的,我没有。如果你想知道
,这只是我在尝试使用wxWidgets框架处理

事件处理程序中的异常时的想法。但我找到了一个更好的

解决方案,至少更多 - 让我们说 - 自然。

Well, it is for me... :-)

To answer your question: Suppose you have an arbitrarily large
inheritance hierarchy with root class A, and you want to know if some
subclass reimplements f. If you know the class, no problem. Just test

&A::f == &B::f

or whatever other class, and it''s done. Now, suppose you don''t know
the class because all you have is a polymorphic pointer to an object
(like in "A* obj"). In this case you would test

&A::f == getFAddress(obj)

With my example code, this would be true if the class of *obj is A,
and false if it''s B. Moreover, you don''t know all the classes from the
inheritance hierarchy, so you can''t (or don''t want to) check your
object against all the classes using typeid, or dynamic_cast, or
whatever.

As for the final purpose of this, I don''t have any. If you want to
know, it''s just an idea I had when trying to deal with exceptions in
event handlers using the wxWidgets framework. But I''ve found a better
solution, at least more -- let''s say -- natural.


这篇关于来自对象的虚拟成员函数的地址(* not * class)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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