0

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?

bochaltura
  • 257
  • 5
  • 14

1 Answers1

2

Yes:

using Derived = Strange::Derived;
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • thanks - is there a way to to it inside Strange class? – bochaltura May 25 '22 at 06:21
  • Secondly, is this "using" approach better than typedef? – bochaltura May 25 '22 at 06:22
  • 1
    @bochaltura No you cannot specify this inside `Strange` because the identifier would then again be scoped to `Strange`. This is more general syntax than `typedef` but in this case does the same as `typedef Strage::Derived Strage;`. – bitmask May 25 '22 at 06:33