【问题标题】:C++ Using templatesC++ 使用模板
【发布时间】:2015-04-24 15:35:16
【问题描述】:

我正在尝试找出模板类。 我声明了一个模板类,它是模板。但现在我想将该字符传递给 PrintChar 类的构造函数。我不想使用模板。但是使用构造函数。我怎么能这样做?我想反转操作。我想以常规构造函数的方式而不是使用模板来完成它。我的结构如下。谢谢。

template<char C>
class PrintChar : public PrintIt {
//something...
};

main()
 PrintChar<'*'> pstuff_star(min,max);
 PrintChar<'~'> pstuff_tilde(min,max);

【问题讨论】:

  • 你是想说你想写PrintChar foo('*')并让它创建一个PrintChar&lt;'*'&gt;吗?

标签: c++ class templates constructor


【解决方案1】:

也许……

class PrintChar : public PrintIt
{
protected:
    char   _ch; //This is char passed to constructor

public:
    PrintChar(char ch, /* other parameters */)
    : _ch(ch)
    {
    }
};

int main()
{
    PrintChar pstuff_star('*', min, max);
    PrintChar pstuff_tilde('~', min, max);

    //...
}

不要过度使用模板。如果您不需要它们,请不要使用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多