0

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)

Mathing
  • 41
  • 5
  • I edited my question.. – Mathing Jun 18 '18 at 10:30
  • shouldn't you implement A::print and make A::printToScreen the pure virtual function that is reimplemented in B? I do not understand what you are trying to achive – choosyg Jun 18 '18 at 10:38
  • Normally, your virtual method would print itself (this), not an argument. Protected only gives visibility through **this**. Realistically, to do what you appear to be trying to do, you would want a B::PrintToScreen() method. – Gem Taylor Jun 18 '18 at 14:32

0 Answers0