use*_*044 0 c++ int primitive constructor
我正在制作一个程序,我需要声明一个构造函数,它可以接受各种原始类型,如double
or float
,并将其转换为int
.
我的程序只使用int
,所以我需要在构造函数中接受任何原始数据类型并将其转换为int
.
您可以使用模板构造函数:
struct Foo
{
template <typename T>
explicit Foo(const T& x) :i(x) {}
private:
int i;
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
81 次 |
最近记录: |