recursive friend classes

后端 未结 4 1903
情深已故
情深已故 2021-01-17 22:03

Is there any way around this:

class B;

class C { 
 public:
  C() { }
 private:
  int i;
  friend B::B();
};

class B { 
 public:
  B() { }
 private:
  int i         


        
4条回答
  •  盖世英雄少女心
    2021-01-17 22:35

    I realize that this is a really silly idea, but couldn't you—theoretically—accomplish this through inheritance, by making the parent class' constructors friends? The code compiles, at least, questionable though it may be.

    class A {
     public:
      A() { }
     private:
      int i;
    };
    
    class D {
     public:
      D() { }
     private:
      int i;
    };
    
    class B : public A {
     public:
      B() { }
     private:
      friend D::D();
    };
    
    class C : public D {
     public:
      C() { }
     private:
      friend A::A();
    };
    

提交回复
热议问题