mardi 24 mai 2022

Using nested class without "parent_class::"

I have a base_class

class Base {

}

class Derived : public Base {
   void derived_function(Strange& strange) {
      strange.private_function(); //inaccessible error as only base is friend
   }
}

class Strange : public StrangeBase{
friend class Base;
private:
  void private_function();
}

I don't like the solution of adding access function to Base class

//I dont like solution below
class Base {
protected:
  access_private_function(Strange& strange) {
    strange.private_function(); // worksn - but I don't like it!
  }
}

as a solution I would like to define all Dervied class as nested to Strange like this

class Strange : public StrangeBase{
friend class Base;
private:
  void private_function();
public:
  class Derived : public Base {
     void derived_function(Strange& strange) {
        strange.private_function(); //now accessible :)
     }
  }
}

However, now when I want to use Dervied externally to Stange class I need to always write the parent class with "::"

  new Strange::Derived()

is there a way to avoid parent:: prefix like in "using namespace std" for example?

Aucun commentaire:

Enregistrer un commentaire