C ++ overload and override

This code generates the following compilation error:

error: there is no corresponding function to call in 'C :: print (int)'

You can help me figure out the procedure that the compiler did to create this error (why did it ignore the function in class B)

#include <iostream>
using std::cout;
class A {
public:
  virtual void print () 
   { cout << "A";}
};
class B : public A {
   int x;
 virtual void print (int y) 
   {cout << x+y;}
};

class C : public B {
public:
  void print ()
   {cout << "C";}
};

int main () {
  C* ptr = new C;
  ptr->print (5);
}
+6
source share
3 answers

Each subsequent definition printhides the parent elements. To show it, you need an operator using:

class A {
public:
  virtual void print () 
   { cout << "A";}
};
class B : public A {
public:
   int x=1;
 using A::print;
 virtual void print (int y) 
   {cout << x+y;}
};

class C : public B {
public:
  using B::print;
  void print ()
   {cout << "C";}
};

Demo

C*, B*. "" , :

B* ptr = new C;

, ... .

+5

() . , C , B.

, :

ptr->B::print (5);
  // ^^^
+1

, (, ) , , (), .

0
source

All Articles