简体   繁体   English

定义并调用类的转换运算符

[英]defining and calling conversion operator of a class

I'm reading the article at : 我在阅读文章:

http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1 http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

and i don't understand some point. 我不明白这一点。 OKAY .How can "Convert" convert itself to any data type? 好的。如何将“转换”自身转换为任何数据类型? What does the float() and double() mean at the end of lines below ?? 在下面的行尾,float()和double()是什么意思? How can converting happen?Could you please explain it with details? 转换如何发生?能否详细解释一下?

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

For those of you who wants related part of the article , I'm copy pasting it below and the related part to my question is at the end . 对于那些想要本文相关部分的人员,我将其复制粘贴到下面,而与我的问题相关的部分在最后。

For sure, 'explicit template argument specification' with method template is also possible. 当然,带有方法模板的“显式模板参数指定”也是可能的。 Consider another example: 考虑另一个示例:

template<class T>
class Convert
{   
   T data;
public: 
   Convert(const T& tData = T()) : data(tData)
   { }

   template<class C>   
   bool IsEqualTo( const C& other ) const      
   {        
       return data == other;   
   }
};

Which can be utilized as: 可以用作:

Convert<int> Data;
float Data2 = 1 ;

bool b = Data.IsEqualTo(Data2);

It instantiates Convert::IsEqualTo with float parameter. 它使用float参数实例化Convert :: IsEqualTo。 Explicit specification, as given below, would instantiate it with double: 如下所示,显式规范将用double实例化它:

bool b = Data.IsEqualTo<double>(Data2);

One of the astounding thing, with the help of templates, you can do it by defining conversion operator on top of template! 令人惊讶的事情之一是,借助模板,您可以通过在模板之上定义转换运算符来做到这一点!

template<class T>
operator T() const
{
    return data;
} 

It would make possible to convert the Convert' class template instance into any type, whenever possible. 只要有可能,就可以将Convert'类模板实例转换为任何类型。 Consider following usage example: 考虑以下用法示例:

Convert<int> IntData(40);
float FloatData;
double DoubleData;


FloatData = IntData;
DoubleData = IntData;

Which would instantiate following two methods (fully qualified names): 它将实例化以下两种方法(完全限定的名称):

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

On one hand it provides good flexibility, since without writing extra code, Convert can convert itself (the specific instantiation) to any data-type - whenever conversion is possible at compilation level. 一方面,它提供了很好的灵活性,因为无需编写额外的代码,只要可以在编译级别进行转换,Convert即可将自身(特定的实例化)转换为任何数据类型。 If conversion is not possible, like from double to string-type, it would raise an error. 如果无法进行转换(例如从双精度类型转换为字符串类型),则会引发错误。


A class type can have user-defined conversions . 类类型可以具有用户定义的转换 This allows you to convert an instance of the class into another type. 这使您可以将类的实例转换为另一种类型。 For example, here we have a struct that when converted to an int always has the value 42: 例如,这里有一个结构,当转换为int时始终具有值42:

struct A {
  operator int() { return 42; }
};

This is a special function, operator int , that is called for conversion to an int. 这是一个特殊的函数,即operator int ,被调用以转换为int。 Now if we have an instance of A, a , we can easily convert it to an int: 现在,如果我们有A的实例a ,我们可以轻松地将其转换为int:

A a;
int x = a; // x now has the value 42

In the article, they are showing how you can use templates to generate a conversion operator for any type. 在本文中,它们显示了如何使用模板为任何类型生成转换运算符。 The function template is defined as: 功能模板定义为:

template<class T>
operator T() const
{
  return data;
}

Now, for whenever you try to convert your object into another type, a new version of that function will be generated, replacing T with the type that you're converting to. 现在,每当您尝试将对象转换为另一种类型时,都会生成该函数的新版本,将T替换为要转换为的类型。 So if we do float f = a , the following function will generated: 因此,如果我们进行float f = a ,将生成以下函数:

operator float() const
{
  return data;
}

This is a function that returns the internal data as a float. 该函数以浮点数形式返回内部数据。

The two lines in the article that are confusing you are just showing you which functions would be generated by the example: 文章中的两行让您感到困惑,它们只是向您显示该示例将生成哪些函数:

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

The first is operator float that converts to a float. 第一个是operator float ,它转换为float。 The second is operator double that converts to a double. 第二个是operator double ,它转换为double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM