C++ 指针一题

输出如下代码结果:

#include <iostream>

using namespace std;

class A {
    int i;
public:
    void Hello() {cout << "Hello" << endl;}
};


int main()
{
    A *p = NULL;
    p->Hello();

    return 0;
}

结果:

Hello

析:

需理解C++的 this指针。将以上C++代码翻译成C程序如下:

#include <stdio.h>

struct A {
    int i;
};

void Hello(struct A *sp)
{
    printf("Hello
");
}

int main()
{
    struct A ×a = NULL;
    Hello(a);

    return 0;
}

实际上C++中指针p,只是一个未使用到的指针而已。

原文地址:https://www.cnblogs.com/aqing1987/p/4284895.html