#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
I understand that objects methods and members variables are stored in different location in memory but when p
is assigned as NULL
how it can resolve to invoke 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>