given the following code:
class A {
protected:
void printToScreen() const;
public:
A() = default;
virtual ~A() {
}
virtual void Print(const A& a) const=0;
};
void A::printToScreen() const {
std::cout << "printToScreen" << std::endl;
}
class B: public A {
public:
B() = default;
void Print(const A& a) const override;
};
void B::Print(const A& a) const {
std::cout << "Print_B" << std::endl;
a.printToScreen(); // *****error*****
}
And for example:
A* a = new B();
a->Print(*a);
I get the following error:
'void A::printToScreen() const' is protected
But I don't understand what it's not correct in my code. printToScreen
is protected, but, I uses this method In a class inherited from A , so what it's the problem?
In addition, what is the different between this->printToScreen();
to a->printToScreen();
? it's seen same (while I talking about the access to protected methods)
How can I fix it? According to the linked answer it's still gives me error, because I can't write like that in class B
:
void Print(const B& a) const override;
The argument must be from type A
- like in class A
(for the override)