First of all, these are the classes that I defined
And in the main() function, I did the followingCode:class A { protected: void print() { cout << "A" << endl; } }; class B : public A { public: void print() { static_cast<A*>(this)->print(); } };
But I get the following error - error C2248: 'A :: print' : cannot access protected member declared in class 'A'Code:int main() { B b; b.print(); return 0; }
But if I make the protected print() function of the class A static as shown below, print() function prints fine.
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.Code:class A { protected: static void print() { cout << "A" << endl; } };