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\n");
}

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

    return 0;
}

 

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

posted @ 2015-02-10 20:47  阿青1987  阅读(101)  评论(0编辑  收藏  举报