ValueT& getdata(OwnerT :: Restricted_Public_Variable){ 返回数据; } class Big { class Private_Restricted_Public_Variable {}; public: Big(int arg):myint(arg) {} Restricted_Public_Variable< int,Big>敏; void f(){getdata(Private_Restricted_Public_Variable())= 3; }}; template<class ValueT, OwnerT> class Restricted_Public_Variable { public: typedef Restricted_Public_Variable Self; private: Restricted_Public_Variable() : data() { } virtual ~Restricted_Public_Variable() { } Restricted_Public_Variable(const ValueT& arg) : data(arg) { } Restricted_Public_Variable(const Self& arg) : data(arg.data) { } const Self& operator=(const ValueT& arg) { data = arg; return *this; }Compiler generated copy constructor, operator=, destructor are fine. Itmakes sense to define any of these functions anyway if you want them to benon-inline. I usually do it for the virtual destructor only though. friend OwnerT; ValueT data;It''s not allowed at present. I think there was a proposal to make it legal,but maybe I am mistaken. As a workaround you can create a public functionthat returns a writable reference to data, and the function will take anOwnerT::Restricted_Public_Variable or other class by value. But the defaultconstructor of this class will be private so that only members of OwnerTwill be able to create OwnerT::Restricted_Public_Variable objects.ValueT& getdata(OwnerT::Restricted_Public_Variable) {return data;} class Big {class Private_Restricted_Public_Variable { }; public: Big(int arg) : myint(arg) { } Restricted_Public_Variable<int, Big> myint;void f() { getdata(Private_Restricted_Public_Variable()) = 3; } }; 这篇关于不允许模板参数成为朋友的基本原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!