As per my knowledge in c++ when initializing an object with a pointer, we have to use new keyword. But in the following example the pointer is not pointing to an object initialized with 'new', but still we can call methods that do not modify member variables.
#include <iostream>
using namespace std;
class B
{
public:
void display()
{ cout<<"Content of base class.\n"; }
};
int main()
{
B *b; // line p : not initialized with 'new'
b->display(); //line q: this prints Content of base class
return 0;
}
In the above code I'm confused why I can call the method in line q in main method. Also why I get an error when I try to access a member variable. Thanks in advance.