C++ implicit type conversion operator operator type of

  • 2020-10-07 18:48:05
  • OfStack

In the reading < < C + + standard library > > When, in section for_each(), encounter the following code,


#include "algostuff.hpp"

class MeanValue{
private:
  long num;
  long sum;
public:
  MeanValue():num(0),sum(0){
  }

  void operator() (int elem){
    num++;
    sum += elem;
  }
  operator double(){
    return static_cast<double>(sum) / static_cast<double>(num);
  }
};

int main()
{
  std::vector<int> coll;
  INSERT_ELEMENTS(coll,1,8);
  double mv = for_each(coll.begin(),coll.end(),MeanValue()); // Implicit type conversion  MeanValue into double
  std::cout<<"mean calue: "<< mv <<std::endl;
}

For operator double(){} in the class, the first time you see this particular function, it is actually the "implicit cast operator" used for type casting.

When a data type conversion is required, 1 is written explicitly as follows:

[

type1 i;
type2 d;
i = (type1)d; // Explicitly write type reversal to convert d from type2 to type1

]

Instead of writing i = d explicitly (type1) to indicate a conversion to the compiler, this requires a "type conversion operator", which can be written abstractly as follows:

operator type(){};

As long as type is a type, including the basic data type, self-written classes or structures can be converted.

The implicit cast operator is a special member function: the operator keyword, followed by a type symbol. You don't have to define the return type of the function, because the return type is the name of the function.

1.operator is used for type conversion functions:

Features of the type conversion function:

1) The type transformation function is defined in the source class;

2) Must be modified by operator, the function name is the target type name or target class name;

3) The function takes no arguments and returns no value, but has an return statement in which it returns target-type data or calls the constructor of the target class.

There are two main types of conversion functions:

1) Object conversion to basic data type:


#include<iostream>
#include<string>
using namespace std;
 
class D{
public:
  D(double d):d_(d) {}
  operator int() const{
    std::cout<<"(int)d called!"<<std::endl;
    return static_cast<int>(d_);
  }
private:
  double d_;
};
 
int add(int a,int b){
  return a+b;
}
 
int main(){
  D d1=1.1;
  D d2=2.2;
  std::cout<<add(d1,d2)<<std::endl;
  system("pause");
  return 0;
}

2) Transformation of objects into objects of different classes:


#include<iostream>
class X;
class A
{
public:
  A(int num=0):dat(num) {}
  A(const X& rhs):dat(rhs) {}
  operator int() {return dat;}
private:
  int dat;
};
 
class X
{
public:
  X(int num=0):dat(num) {}
  operator int() {return dat;}
  operator A(){
    A temp=dat;
    return temp;
  }
private:
  int dat;
};
 
int main()
{
 X stuff=37;
 A more=0;
 int hold;
 hold=stuff;
 std::cout<<hold<<std::endl;
 more=stuff;
 std::cout<<more<<std::endl;
 return 0;
}

The X class in the above program converts an X object to an A object by casting the type "operator A()".


Related articles: