模板铸造操作员 [英] Template casting operator
问题描述
您好,
为什么我的演员来自Vector< class Float>到矢量< float>不行?它是
不会编译,
模板< class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1) ,z(z1){}
inline Vector< float> operator()()const;
Float x,y,z;
};
template< ;类Float> inline Vector< float>
Vector< Float> :: operator()()const
{
返回Vector< float>((float )x,(float)y,(float)z);
}
int main()
{
Vector< double> pd(5.6,3.4,2.4);
Vector< float> PF =(矢量<浮子>)PD; / *编译器错误在这里* /
}
我非常希望能够投射一个Vector< double>到一个Vector< float> ;.
Hello,
Why does my cast from Vector<class Float> to Vector<float> not work? It
won''t compile,
template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1),z(z1){}
inline Vector<float> operator() () const;
Float x,y,z;
};
template <class Float> inline Vector<float>
Vector<Float>::operator() () const
{
return Vector<float>((float)x,(float)y,(float)z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4);
Vector<float> pf=(Vector<float>)pd; /* compiler error here */
}
I''d ideally like to be able to cast a Vector<double> to a Vector<float>.
推荐答案
" Makhno" < RO ** @ 127.0.0.1>写道...
"Makhno" <ro**@127.0.0.1> wrote...
为什么我的演员来自Vector< class Float>到矢量< float>不行?
因为你没有使用运算符()你写的。但那不是什么
困扰你,是吗?从Vector< type1>转换到矢量< type2>
是一个可以很容易获得的东西,给定正确的功能。
见下文。
它
不会编译,
模板< class Float> class Vector
公共:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1),z(z1){}
内联矢量<浮起> operator()()const;
在没有提供身体的情况下声明''内联''是没用的。
回到你的问题......你可能想要创建一个模板化的
类型转换运算符:
模板< class S> operator Vector< S>()const;
Float x,y,z;
};
template< class Float> inline Vector< float>
Vector< Float> :: operator()()const
{
返回Vector< float>((float)x,(float)y,(float)z ); $
}
制作这个
模板< class F>模板< class D>
向量< F> ::运算符向量< D>()const
{
返回向量< D>( x,y,z);
}
int main()
{
Vector< double> pd(5.6,3.4,2.4);
Vector< float> PF =(矢量<浮子>)PD; / *编译错误在这里* /
立即试用。
}
我非常希望能够施放Vector< double>到一个Vector< float>。
Why does my cast from Vector<class Float> to Vector<float> not work?
Because you didn''t use the operator() you wrote. But that''s not what
bothers you, is it? Conversion from Vector<type1> to Vector<type2>
is something that can be easily obtained, given the right function.
See below.
It
won''t compile,
template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1),z(z1){}
inline Vector<float> operator() () const;
Declaring something ''inline'' without providing its body is useless.
Back to your problem... You probably wanted to create a templated
type conversion operator:
template<class S> operator Vector<S>() const;
Float x,y,z;
};
template <class Float> inline Vector<float>
Vector<Float>::operator() () const
{
return Vector<float>((float)x,(float)y,(float)z);
}
Make this
template<class F> template<class D>
Vector<F>::operator Vector<D>() const
{
return Vector<D>(x, y, z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4);
Vector<float> pf=(Vector<float>)pd; /* compiler error here */
Try it now.
}
I''d ideally like to be able to cast a Vector<double> to a Vector<float>.
如果提供适当的转换,则不需要C风格的演员表。
--- -------------------------编译的代码---------------------- ---
模板< class F> class Vector
{
public:
Vector(F x1,F y1,F z1):x(x1),y(y1) ,z(z1){}
模板< class D>运算符Vector< D> ()const;
F x,y,z;
};
模板< class F>模板< class D>
Vector< F> :: operator Vector< D> ()const
{
返回Vector< D>(x,y,z);
}
int main()
{
Vector< double> pd(5.6,3.4,2.4);
Vector< float> pf = pd; / *这里没有编译错误* /
}
------------------------- --------------------------------------------------
Victor
C-style casts are unnecessary if proper conversion is provided.
---------------------------- code that compiles -------------------------
template<class F> class Vector
{
public:
Vector(F x1, F y1, F z1):x(x1),y(y1),z(z1){}
template<class D> operator Vector<D> () const;
F x,y,z;
};
template <class F> template<class D>
Vector<F>::operator Vector<D> () const
{
return Vector<D>(x,y,z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4);
Vector<float> pf = pd; /* no compiler error here */
}
---------------------------------------------------------------------------
Victor
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
news:43gUb.174338
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:43gUb.174338
Rc4.1320258@attbi_s54 ...
Rc4.1320258@attbi_s54...
" Makhno" < RO ** @ 127.0.0.1>写道...
"Makhno" <ro**@127.0.0.1> wrote...
为什么我的演员来自Vector< class Float>到矢量< float>不
Why does my cast from Vector<class Float> to Vector<float> not
工作?
< snip>
回到你的问题...你可能想创建一个模板化的
类型转换运算符:
模板< class S>运算符Vector< S>()const;
work?
<snip>
Back to your problem... You probably wanted to create a templated
type conversion operator:
template<class S> operator Vector<S>() const;
当然,您的诊断是正确的,并且解决方案有效,但是
不会''在这个
案例中定义转换构造函数更自然吗?
如果我无法修改转换运算符,我会写一个转换运算符
目标类型的定义(例如转换为int或指针
类型)或者如果我使用运算符我可以避免构建新的
对象(例如通过返回对成员的引用。)对于同一模板的特化之间的转换
,我倾向于使用
转换构造函数。
Jonathan
Your diagnosis is correct, of course, and you solution works, but
wouldn''t it be more natural to define a converting constructor in this
case?
I''d write a conversion operator if I was not able to modify the
definition of the target type (e.g. converting to int or a pointer
type) or if I by using an operator I could avoid constucting a new
object (e.g. by returning a reference to a member.) With conversions
between specializations of the same template, I''d tend to use a
converting constructor.
Jonathan
这篇关于模板铸造操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!