我正在使用 Eckel 的“Thinking in C++”学习 C++。它声明如下:
- 如果一个类包含虚方法,则为该类创建一个虚函数表等。大致解释了函数表的工作原理。(我知道 vtable 不是强制性的,但 Visual C++ 会创建一个。)
- 调用对象作为参数传递给被调用函数。(这可能不适用于 Visual C++(或任何编译器)。)我试图找出 VC++ 如何将调用对象传递给函数。
为了在 Visual C++ 中测试这两个点,我创建了以下类(使用 Visual Studio 2010,WinXP Home 32bit):
字节检查器.h:
#pragma once
class ByteExaminer
{
public:
short b[2];
ByteExaminer(void);
virtual void f() const;
virtual void g() const;
void bruteFG();
};
ByteExaminer.cpp:
#include "StdAfx.h"
#include "ByteExaminer.h"
using namespace std;
ByteExaminer::ByteExaminer(void)
{
b[0] = 25;
b[1] = 26;
}
void ByteExaminer::f(void) const
{
cout << "virtual f(); b[0]: " << hex << b[0] << endl;
}
void ByteExaminer::g(void) const
{
cout << "virtual g(); b[1]: " << hex << b[1] << endl;
}
void ByteExaminer::bruteFG(void)
{
int *mem = reinterpret_cast<int*>(this);
void (*fg[])(ByteExaminer*) = { (void (*)(ByteExaminer*))(*((int *)*mem)), (void (*)(ByteExaminer*))(*((int *)(*mem + 4))) };
fg[0](this);
fg[1](this);
}
The navigation through the vtable in bruteFG()
works - when I call fg[0](this)
, f()
is called. What does NOT work, however, is the passing of this
to the function - meaning that this->b[0]
is not printed correctly (garbage comes out instead. I'm actually lucky this doesn't produce a segfault).
So the actual output for
ByteExaminer be;
be.bruteFG();
is:
virtual f(); b[0]: 1307
virtual g(); b[1]: 0
So how should I proceed to get the correct result? How are the this
pointers passed to functions in VC++?
(Nota bene: I'm NOT going to program this way seriously, ever. This is "for the lulz"; or for the learning experience. So don't try to convert me to proper C++ianity :))