C++ 指向类中虚函数的指针以及一个非一般指针的定义
C++ 指向类中虚函数的指针以及一个特殊指针的定义
一个类,很简单的
问题:定义一个指向func的指针,如果输出指针值,应该是个偏移量才对,为什么却是个内存地址?
第二个问题是,如何定义指向析构函数的成员指针?
------解决方案--------------------
1. 应该是个偏移量?!
这个知识点是从哪儿学的?
http://blog.****.net/mougaidong/article/details/6894563
2. http://stackoverflow.com/questions/10858998/how-do-i-get-the-member-function-pointer-of-a-destructor
------解决方案--------------------
忘记帖地址了:
http://edu.21cn.com/ncre/g_51_754671-1.htm
百度搜索到的. 这里有实现的代码, 但没有通用性. 因为这个东西就不应该是标准的.
------解决方案--------------------
怎么会没有关系.
你的程序在运行时, 函数的代码肯定是会放到具体内存中, 那么这个地址肯定会是有一个"基址"存在的.
如果程序还没有运行, 那么这个基址就不存在, 那应该是一个0起的偏移,. 但是一但放入了内存中运行, 就存在基址了, 所以你得到的函数地址应该就是这个函数在内存中的入口地址了.
------解决方案--------------------
12.4.2 A destructor is used to destroy objects of its class type. A destructor takes no parameters, and no return type
can be specified for it (not even void). The address of a destructor shall not be taken. A destructor shall
not be static. ...
不允许取析构函数的地址,而且没有返回值(甚至连void都没有)
------解决方案--------------------
至于构造函数:
12.1.10 No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken.
------解决方案--------------------
19L and 20L正解, 不可以取构造函数与析构函数的地址。
成员指针如何实现,是编译器的实现细节, 了解就好,不必深究。
一个类,很简单的
class A
{
public:
A(){}
~A(){}
virtual void func(){}
void func2(){}
int _intn;
};
//main
void (A::*pfunc)()=&A::func;//虚函数
void (A::*pfunc2)()=&A::func2;//非静态成员函数
int A::*p_intn=&A::_intn;//成员变量
printf("%d\n",pfunc);
printf("%d\n",pfunc2);
printf("%d\n",p_intn);
问题:定义一个指向func的指针,如果输出指针值,应该是个偏移量才对,为什么却是个内存地址?
第二个问题是,如何定义指向析构函数的成员指针?
------解决方案--------------------
1. 应该是个偏移量?!
这个知识点是从哪儿学的?
http://blog.****.net/mougaidong/article/details/6894563
2. http://stackoverflow.com/questions/10858998/how-do-i-get-the-member-function-pointer-of-a-destructor
------解决方案--------------------
忘记帖地址了:
http://edu.21cn.com/ncre/g_51_754671-1.htm
百度搜索到的. 这里有实现的代码, 但没有通用性. 因为这个东西就不应该是标准的.
------解决方案--------------------
怎么会没有关系.
你的程序在运行时, 函数的代码肯定是会放到具体内存中, 那么这个地址肯定会是有一个"基址"存在的.
如果程序还没有运行, 那么这个基址就不存在, 那应该是一个0起的偏移,. 但是一但放入了内存中运行, 就存在基址了, 所以你得到的函数地址应该就是这个函数在内存中的入口地址了.
------解决方案--------------------
12.4.2 A destructor is used to destroy objects of its class type. A destructor takes no parameters, and no return type
can be specified for it (not even void). The address of a destructor shall not be taken. A destructor shall
not be static. ...
不允许取析构函数的地址,而且没有返回值(甚至连void都没有)
------解决方案--------------------
至于构造函数:
12.1.10 No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken.
------解决方案--------------------
19L and 20L正解, 不可以取构造函数与析构函数的地址。
成员指针如何实现,是编译器的实现细节, 了解就好,不必深究。