0
在下面的例子中,我很難理解函數指針對成員函數的調用。由編譯器代替成員函數的函數指針的代碼
(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-the-hidden-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;
}
感謝
_I想知道編譯器爲這兩個函數調用生成的代碼_和..你不能編譯成彙編,並尋找自己,因爲......? –
我不是說彙編代碼。我對時尚感興趣。像obj.Fun()被Fun(&obj) –
取代你的無效投射產生UB。 – Jarod42