我对C++编译器如何处理虚函数中的“this”指针有一个困惑,我的代码是:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace std;
class A {
public:
void print_A(void) {
cout << "A::THIS = " << this << endl;
return;
}
int a;
virtual ~A(void) {
return;
}
};
class B {
public:
virtual void print(void) {
cout << "B::THIS = " << this << endl;
return;
}
int b;
};
class C: public A, public B {
public:
void print(void) {
cout << "C::THIS = " << this << endl;
return;
}
int c;
};
class D {
public:
int d;
virtual ~D(void) {
return;
}
};
class E: public D, public C {
public:
int e;
virtual ~E(void) {
return;
}
};
void global_print(B *pb) {
cout << "pb = " << pb << endl;
pb->print();
return;
}
int main(int argc, char *argv[]) {
E e;
A *pa = &e;
B *pb = &e;
C *pc = &e;
D *pd = &e;
E *pe = &e;
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "pc = " << pc << endl;
cout << "pd = " << pd << endl;
cout << "pe = " << pe << endl;
pb->print();
pc->print();
pe->print();
global_print(&e);
return 0;
这是在我的Linux中用G++编译的结果:
pa = 0x7ffc1e7c7a80
pb = 0x7ffc1e7c7a90
pc = 0x7ffc1e7c7a80
pd = 0x7ffc1e7c7a70
pe = 0x7ffc1e7c7a70
C::THIS = 0x7ffc1e7c7a80
C::THIS = 0x7ffc1e7c7a80
C::THIS = 0x7ffc1e7c7a80
pb = 0x7ffc1e7c7a90
C::THIS = 0x7ffc1e7c7a80
我无法理解我将&e
发送到global_print
而global_print
中的pb
等于0x7FFC1E7C7A90
。然后调用pb->print()
,global_print
中的这个指针是0x7FFC1E7C7A80
,与pc
相同。似乎pb->print()
将B类转换为C类,但B类是C类的父类,如何转换?
虽然pb可以通过虚函数获得C::print
的地址,但是要将B类转换为C类,需要知道的只是一个没有足够信息的地址,那么程序在运行时是如何知道B类转换为C类的呢?
多谢!祝你有一个愉快的一天!
#include<iostream>
class D
{
public:
D() {
std::cout << "This is constructor for D class." << std::endl;
};
~D() {
std::cout << "This is destructor for D class." << std::endl;
};
protected:
int variable = 123456;
};
class B :
virtual public D
{
public:
B() {
std::cout << "This is constructor for B class." << std::endl;
};
~B() {
std::cout << "This is destructor for B class." << std::endl;
};
private:
};
class C :
virtual public D
{
public:
C() {
std::cout << "This is constructor for C class." << std::endl;
};
~C() {
std::cout << "This is destructor for C class." << std::endl;
};
private:
};
class A :
public B, public C
{
public:
A() {
std::cout << "This is constructor for A class." << std::endl;
};
~A() {
std::cout << "This is destructor for A class." << std::endl;
};
void function() {
std::cout << "Variable: " << variable << std::endl;
}
private:
};
int main() {
A a;
std::cout << std::endl;
a.function();
std::cout << std::endl;
return 0;
}
编译器不知道哪个类的变量或方法必须继承。为此,我们使用virtual关键字。我们对编译器说,由于virtual关键字,编译器只继承一个成员。
class B : virtual public D
......
class C : virtual public D
......
尽可能避免多重继承是必须的!
代码输出:
This is constructor for D class.
This is constructor for B class.
This is constructor for C class.
This is constructor for A class.
Variable: 123456
This is destructor for A class.
This is destructor for C class.
This is destructor for B class.
This is destructor for D class.