代码由编译器代替,用于成员功能的功能指针
Code replaced by compiler for Function pointers of member functions
我真的很难理解函数指针在以下示例中相对于成员函数。
(f.*(FPTR) bp)(0); // This call b()
(b.*(BPTR) fp)(0); // This call f()
我想知道替换的代码(众所周知,诸如obj.fun()之类的函数呼叫被娱乐替换(& obj)由编译器由这两个函数在这些函数时替换成员功能是虚拟和非虚拟的。有人帮我理解吗?
我想更多地了解此链接说明:http://www.learncpp.com/cpp-tutorial/8-8-thenden-hidded-hidded-thide-this-pointer/
#include <iostream>
using std::cout;
using std::endl;
class Foo
{
public:
void f(int i = 0)
{
cout << "Foo" << endl;
}
};
class Bar
{
public:
void b(char c = 'b')
{
cout << "Bar" << endl;
}
};
int main()
{
typedef void (Foo::*FPTR) (int);
typedef void (Bar::*BPTR) (char);
FPTR fp = &Foo::f;
BPTR bp = &Bar::b;
Foo f;
Bar b;
/*
* we are casting pointer to non-compatible type here
* Code works, but want to know how it is.
*/
(f.*(FPTR) bp)(0);
(b.*(BPTR) fp)(0);
return 0;
}
谢谢
您的代码显示未定义的行为,这意味着编译器可以执行自己喜欢的任何事情,包括您的(错误)期望做。
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 升压信号2将插槽传递到成员功能以断开连接
- 多次实现成员功能
- 如何定义可变参数类模板的成员模板功能
- 私有成员数据不可用于公共成员功能
- 好友功能 - 成员无法访问
- 如何启用友元类的友元功能直接在C++中访问其私有成员
- 无法从好友功能访问类的私有成员?"ostream"不是"std"的成员?
- C++从成员类型中扣除类型的功能模板?
- 如何避免大多数成员功能相同的代码重复
- 无法访问公共功能? 没有指定成员?
- C 成员功能
- =删除用户定义的成员功能,除了构造函数,分配运算符C 11
- C 朋友课程和朋友成员功能
- C 函数调用包装器包含类成员功能作为模板参数
- 共享库中静态功能成员的破坏顺序
- 可以使用仅功能成员属性进行回调界面
- 虚拟方法与std ::功能成员变量在性能方面
- 虚拟继承和功能成员
- 指向功能成员和非成员的指针