0

I am trying to use a function from base class to child class but couldn't figure out how to do this.

This is my simplified program :

class CPerson
{
public:
  CPerson() = default;
  void print() {std::cout <<"test"<<std::endl;}
};

class CStudent : private CPerson
{
public:
  CStudent() = default;
};

class CTeacher : private CPerson
{
  public:
  CTeacher() = default;
};

class CTutor : public CTeacher , public CStudent 
{
  public:
  CTutor() = default;

  void test()
  { 
        CPerson::print(); //Error is here
  }
};

So the error that I had first was

base class "CPerson" is ambiguous 

To fix this issue I had to change class CStudent : public CPerson to class CStudent : private CPerson and class CTeacher : public CPerson to class CTeacher : private CPerson . But in this case I will get another error ,

`type "CPerson::CPerson"  is inaccessible`

and to fix this I have to change the classes again to public instead of private . But I will end again with the first error . So I'm kinda stuck here

Samir
  • 235
  • 1
  • 10
  • 1
    `CTeacher` and `CStudent` are inherited from `CPerson`. Hence, scope `CPerson::` doesn't add anything valuable to the compiler to resolve this. Try `CTeacher::` or `CStudent::` to guide the compiler into the intended direction. – Scheff's Cat Nov 28 '20 at 09:27
  • @Scheff and what happens if CTeacher and CStudent already have their print functions – Samir Nov 28 '20 at 09:50
  • 1
    Whooah... That's tricky. I tried to chain scopes but this didn't work. (Actually, I didn't really expect this.) However, I found a quite simple hack applying pointer casts to `this`, and these can be chained: [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/8deb045966db79e0). Please, note that I tried to solve it as a kind of puzzle. In real life, I would stop when multiple inheritance introduces the same base class multiple times not to mention that I use/need multiple inheritance rather rarely. (Usually, I try to prevent all what makes code more complicated than necessary.) ;-) – Scheff's Cat Nov 28 '20 at 11:47

0 Answers0