c++ - 如何将这个模板化的类函数声明为朋友?
问题描述
模板让事情变得如此困难。我没有正确的语法,我已经尝试了很多年。我在一个名为“ModelMngr”的类中有一个名为“createSphere”的函数,我想成为另一个类的朋友。
class ModelMngr
{public:
template <typename T_scalar>
static Sphere<T_scalar>* createSphere(T_scalar radius, int sides, int heightSegments, const String& name = "Sphere Primitive Mesh");
}
template <typename T_scalar>
class Sphere : public MeshBaseClass<T_scalar>
{public:
template <typename T> friend Sphere<T>* ModelMngr::createSphere(T radius, int sides, int heightSegments, const String& name = "Sphere Primitive Mesh");
}
我将朋友声明中的 T_scalar 更改为 T,因为我担心会与封闭类的模板参数发生名称冲突。有没有冲突?
解决方案
转发声明Sphere
,从朋友声明中删除默认参数并在类定义后添加分号:
template <typename T_scalar>
class Sphere;
class ModelMngr {
public:
template <typename T_scalar>
static Sphere<T_scalar> *
createSphere(T_scalar radius, int sides, int heightSegments,
const String& name = "Sphere Primitive Mesh");
};
template <typename T_scalar>
class Sphere : public MeshBaseClass<T_scalar> {
public:
template <typename T>
friend Sphere<T> *
ModelMngr::createSphere(T radius, int sides, int heightSegments,
const String &name);
};
推荐阅读
- python - 在文本文件中每次出现的两个子字符串之间提取文本
- r - 用R中的条件日期列填充一列
- c# - 寻找一种从另一个插件访问全局变量的方法
- java - 将 findElementById("elementID") 与 appium 一起用于原生 android 应用程序。错误:“不支持定位器策略‘css 选择器’”
- javascript - 在赛普拉斯中随时随地创建特定大小的文件
- javascript - 在同一页面中有多个反应包的性能影响
- regex - 在正则表达式/日食搜索中查找不包含双斜杠的行
- javascript - Http Post 发送大列表在生产服务器中不起作用
- android - Kotlin Android Studio 未使用的导入指令
- python - 不区分大小写的 argparse 选项,而不会丢失选项列表中的大小写信息