C++程序到C程序的翻译
class CCar {
public:
int price;
void SetPrice(int p);
};
void CCar::SetPrice(int p) {price = p}
main() {
CCar car;
car.SetPrice(2000);
}
最初没有C++编译器,所以要把C++程序翻译成C程序执行。上面的程序会被翻译如下:
struct CCar {
int price;
};
void SetPrice(struct CCar* this, int p) {
this->price = p;
}
main() {
struct CCar car;
SetPrice( &car, 2000);
}
- But!
claas A {
int i;
public:
void Hello() {cout << "hello" << endl;}
}; ——> void Hello(A * this){ cout << "hello" << endl; }
main() {
A * p = NULL;
p->Hello(); ——> Hello(p); // 不会出错
} // 输出:hello