本文介绍了成员表示的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从成员函数中做一些回调,一切正常,直到我试图使用从2类派生的模板类作为回调对象,当我得到以下错误:
错误C2440:'reinterpret_cast':成员的指针有不同的表示;不能在它们之间转换
这个东西表示成员函数指针有不同的表示(doh!)
这些表示是什么?它们之间有什么区别?
解决方案
struct A
{
int x;
void f();
};
int A :: * pmi =& A :: x;
void(A :: * pmf)()=& A :: f;
int n = sizeof(pmi); // 8 byte with my compiler
int m = sizeof(pmf); //我的编译器为12字节
I'm trying to make some callbacks from member functions and everything was ok until I tried to use a template class derived from 2 classes as callback object when I got the following error:
error C2440: 'reinterpret_cast' : Pointers to members have different representations; cannot cast between them
This thing signaled me that member function pointers have different representations(doh!)
What are these representations? What is the difference between them?
解决方案
Danny Kalev explains this quite nicely:
struct A
{
int x;
void f();
};
int A::*pmi = &A::x;
void (A::*pmf)() = &A::f;
int n = sizeof (pmi); // 8 byte with my compiler
int m = sizeof (pmf); // 12 bytes with my compiler
这篇关于成员表示的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!