如何使用具有继承和模板的友元类
How can I use friend classes with inheritance and templates
我有一个特殊的配置要构建,我不知道如何编写:
template <typename VarType>
class A
{
protected:
VarType m_myVar;
}
template <typename VarType>
class B : public A<VarType>
{
}
class C : public B<SpecialType>
{
void DoSomething()
{
m_myVar.PrivateFunction();
}
}
class SpecialType
{
private:
void PrivateFunction()
{
//Do something
}
}
如何使用关键字friend使其工作??
谢谢你的回答。
只需将C
声明为SpecialType
的朋友。。。
class SpecialType
{
private:
friend class C;
void PrivateFunction()
{
//Do something
}
};
在理想的情况下,您可以在SpecialType类decl中编写friend C::DoSomething();
,但遗憾的是,你唯一的选择似乎是friend class C;
(根据nyrl)
不完全类型的friends和forward声明并不像我们希望的那样好。
相关文章:
- 如何使用单独文件中的派生类访问友元函数对象
- 如何在嵌套类中正确使用友元声明?
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 基类中受保护的纯虚函数如何被基类的友元类使用?
- 运算符重载在 C++ 中使用友元函数
- C++:成员不可访问-使用友元函数允许一个类修改另一个类的成员数据
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- 在C++中使用友元函数操作模板类中的私有变量时会发生错误
- 不能在友元函数中使用重载运算符
- 使用shared_ptr向量重载 ostream 友元运算符
- 使用模板类重载的友元运算符
- C++ 友元函数在内存位置上有何不同?
- 使用友元函数重载新运算符
- 什么时候使用友元类/函数而不是继承
- 如何使用来自友元函数的嵌套类?
- 为什么友元运算符<<在某些情况下使用,而在其他情况下不使用
- 使用友元函数从多态类中检索类型信息
- 使用友元类减少编译时间和依赖关系
- 关于使用友元函数的复数