类成员方法 - 返回类型模板
Class member method - return type template
是否可以在 C++11 中以某种干净的方式执行此操作
class Foo
{
public:
Foo(Bar1 * b1, Bar2 * b2) : bar1(b1), bar2(b2) {}
template <typename T> T * GetData<T>();
private:
Bar1 * bar1;
Bar2 * bar2;
};
template <> Bar1 * Foo::GetData<Bar1>() { return this->bar1;}
template <> Bar2 * Foo::GetData<Bar2>() { return this->bar2;}
在主代码中
Foo * foo = new Foo(new Bar1(), new Bar2());
Bar1 * b1 = foo->GetData<Bar1>();
Bar2 * b2 = foo->GetData<Bar2>();
这样做,它不会编译。
删除模板成员声明中的<T>
:
template <typename T> T* GetData();
而不是:
template <typename T> T* GetData<T>();
相关文章:
- 从私有成员变量的成员方法返回unique_ptr
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 在模板化成员函数的返回类型中使用 std::enable_if 时的编译器差异
- 将成员函数的返回类型引用到C++中的自定义类
- 具有引用返回类型的重写方法上的协变返回类型无效
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 如何对绑定的成员方法进行typedef,然后将该类型用作模板参数
- 在类定义中推导内联静态成员函数的返回类型
- 如果函数是在类的主体中定义的,我是否需要在成员函数的返回类型中指定 typename?
- Java 调用 dll 字符串返回类型方法
- 推断成员函数的返回类型
- 具有协变返回类型的方法在 VC++ 上崩溃
- 解释C 中三种返回类型的方法
- 从成员函数指针获取方法的返回类型
- 类成员方法 - 返回类型模板
- c++:继承具有不同(但协变)返回类型的方法
- 静态成员方法应该返回什么类型的指针?
- 为什么c#或c++不能根据返回类型区分方法