简体   繁体   中英

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

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 AI 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.

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM