A recent question on SO made me think about the following.
Consider the following code:
class Base{
public:
void print() { cout<<"In Base"<<endl;}
};
class Derived: public Base{
public:
void print() { cout<<"In Derived"<<endl;}
};
int main(void)
{
Base *bp;
Derived ob;
bp = &ob;
(*bp).print(); // prints: In Base
ob.print(); // print: In Derived
return 0;
}
Why does (*bp),print()
doesn't behave similar to ob.print()
.
I think that (*bp) should give back the object ob
since bp
is referred to object ob
and when we de-reference using *
operator all that is we get the value at that address and the ob
object is stored at the address present in bp
. So the first function call should be same as the send one.
Please clarify the concept.