issues with accessing parent member functions by typecasting this pointer
First of all, these are the classes that I defined
Code:
class A
{
protected:
void print() { cout << "A" << endl; }
};
class B : public A
{
public:
void print() { static_cast<A*>(this)->print(); }
};
And in the main() function, I did the following
Code:
int main()
{
B b;
b.print();
return 0;
}
But I get the following error - error C2248: 'A :: print' : cannot access protected member declared in class 'A'
But if I make the protected print() function of the class A static as shown below, print() function prints fine.
Code:
class A
{
protected:
static void print() { cout << "A" << endl; }
};
Why does it work only when print() is declared static? I think it might have to do with the casting and all that, but I wasn't exactly sure. I would appreciate for any help. Thanks in advance.