How do I access from template class A a struct declared as private field in template B class in c++?

123 Views Asked by At

There is following c++ raw code:

template<typename T>
class A {
 private:
  // here I want to access B::SomeStruct to create SomeStruct field in class A
};

template<typename T>
class B {
 private:
  template<typename Tp>
  friend class A;

  struct SomeStruct {
    void some_field;
  };
};

In class A I want to create a field with the type of SomeStruct - struct declared in class B as a private member. Does it even possible?

Restrictions:

  1. Forbidden to create a global struct, accessible both A and B classes.
  2. Forbidden to create any public fields in class B.
1

There are 1 best solutions below

0
On BEST ANSWER

This would be one way. All B:s befriends all A:s and A<T> has a B<T>::SomeStruct member:

template<typename T>
class B {
private:
    template<typename Tp>
    friend class A;

    struct SomeStruct {};
};

template<typename T>
class A {
private:
    // 'typename' prior to dependent type name 'B<T>::SomeStruct':
    typename B<T>::SomeStruct ss;
};