【问题标题】:How to make a type out of a template class in C++?如何从 C++ 中的模板类中创建类型?
【发布时间】:2015-10-16 10:49:38
【问题描述】:

假设我有一个名为 ptr 的 template 类。

我可以像这样使用 ptr 并且它可以工作:

int* data = new int(55);
ptr<int> data_ptr = ptr<int>(data);

但我想这样使用它:

int* data = new int(55);
int_ptr data_ptr(data);

简而言之,我如何标记 int_ptr 以使其等于 ptr&lt;int&gt; ?

我尝试了 typedef 和 typename 但找不到有效的解决方案。

【问题讨论】:

  • “我尝试了 typedef 和 typename 但找不到有效的解决方案。”,什么失败了?
  • 为什么typedef ptr&lt;int&gt; int_ptr; 不起作用?
  • 我也把“模板”放在那一行,那一定是错误。感谢您的回答。我对 C++ 中的这个模板概念有些陌生。

标签: c++ oop templates typedef typename


【解决方案1】:
using int_ptr = ptr<int>;

或

typedef ptr<int> int_ptr;

是你需要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2014-12-21
    • 2014-11-05
    相关资源
    最近更新 更多