#include <iostream>
class Test {
public:
int i;
void print()
{
std::cout << "Hello" << std::endl;
}
};
int main()
{
class Test *p = NULL;
p->print();
(*p).print();
}
Output:
Hello
Hello
我了解对象方法和成员变量存储在内存中的不同位置,但是何时p
分配为NULL
如何解析调用Test::print()
Test6:~ 1001> g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Test6:~ 1002> g++ manoj.cpp
Test6:~ 1003> ./a.out
Hello
Hello
Test6:~ 1004> cat manoj.cpp
#include <iostream>
class Test {
public:
int i;
void print()
{
std::cout << "Hello" << std::endl;
}
};
int main()
{
class Test *p = NULL;
p->print();
(*p).print();
}
Test6:~ 1005>