模板类 C++ - 排除某些类型 [英] Template Class C++ - exclude some types
问题描述
我最近创建了一个工作正常的模板类.
i recently created a template class which is working fine.
现在我想使用const int"(例如),但禁止动态绑定.
Now i wanted to use "const int" (for example), but dynamic binding is forbidden.
是否有可能排除 const int 类型?
is there a possibility to exclude the type const int?
这是我的课;编译器将丢弃第二个构造函数的错误.我已经看到了,但我只是不知道如何以正确的方式修改它 - 和想法?
this is my class; the compiler will drop out an error for the 2nd constructor. i've seen that one coming, but i just don't know how to modify it the correct way - and ideas?
template <class T>
class Vector2D
{
public:
T X;
T Y;
Vector2D()
{
X = 0;
Y = 0;
};
Vector2D(T x, T y)
{
X = x;
Y = y;
};
}
推荐答案
使用成员初始化列表:
template <class T>
class Vector2D
{
public:
T X;
T Y;
Vector2D(): X(0), Y(0)
{
};
Vector2D(T x, T y):X(x),Y(y)
{
};
}
那应该可以用 const int
解决您当前的问题.如果您真的对禁止某种类型感兴趣,请查看 boost::enable_if.
That should solve your current problem with const int
. If you're really interested in forbidding a certain type in general, take a look at boost::enable_if.
这篇关于模板类 C++ - 排除某些类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!