两个c++编译器对c++对象模型和虚函数调用方式的实现

写了个小程序在linux(64位)和windows nt(32位)上测试了一下,

只是感兴趣,对工作没什么实际价值,呵呵
#include #include using namespace std; class A { public: A(){} ~A(){} virtual void test(const string& s) = 0; }; class B:public A { public: B():m_data(100){} ~B(){} virtual void test(const string& s) { cout << "test in B " << endl; //cout << m_data << endl; //cout << s; } private: int m_data; }; int main() { B t; cout << sizeof(t) << endl; void (* func)(const string&); func = (void (*)(const string&))*(long*)* (long*)&t; func("hello"); }

 

compiler1: g++ 3.4.6

compiler2: vc++ 7.1

 

在两种平台下程序都可以“正常”运行,看来两者都将虚函数表指针放在对象"最前面",

且thiscall调用约定都是将this指针存放在寄存器中(猜测),而不是作为一个普通参数传递。

两者参数传递方式也不同,cout<

短参数传递作了优化

 

 

你可能感兴趣的:(c++,编译器,compiler,string,windows,linux)