考虑这个类:
template <typename t>
class Something
{
public:
Something(t theValue) {mValue=theValue;}
t mValue;
};
当我这样做时,一切都很好:
Something<float>* mSomething=new Something<float>(100);
但是,如果我尝试这样做:
Something<float> mSomething(100);
我在另一个类中声明上述内容 - 即
class SomethingElse
{
public:
Something<float> mSomething(100);
};
它告诉我,我放在括号内的任何内容都是语法错误。
这里必要的语法到底是什么——或者这是模板的一些怪癖,因此不可能?
您不能将这样的初始化程序用作函数调用
class SomethingElse
{
public:
Something<float> mSomething(100);
};
您需要使用相等或大括号的初始化程序。例如
class SomethingElse
{
public:
Something<float> mSomething { 100 };
};
这是一个演示程序。
template <typename t>
class Something
{
public:
Something(t theValue) {mValue=theValue;}
t mValue;
};
class SomethingElse
{
public:
Something<float> mSomething{ 100 };
};
int main()
{
}
根据 C++ 语法
member-declarator:
declarator virt-specifier-seqopt pure-specifieropt
declarator brace-or-equal-initializeropt
identifieropt attribute-specifier-seqopt: constant-expression
请参阅术语大括号或相等初始化器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句