Operator type casts member function resolution in C++

  • 2020-04-02 01:30:44
  • OfStack

The type conversion operator is a special class member function that defines the conversion of a value of a class type to a value of another type. The transform operator is declared in the body of the class definition, followed by the target type of the transform after the reserved word operator. The cast function, also known as a type cast member function, is a non-static member function in a class. Its definition format is as follows:


   class < Type specifier 1>
    {
     public:
      operator < Type specifier 2>();
 ... 
    }

This transformation function defines the function by < Type specifier 1 > to < Type specifier 2 > Mapping between. As you can see, conversion functions are used to convert data of one type into another type.

1. Operator is used for type conversion functions:

Features of the type conversion function:

1) 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 has no parameters and no return value, but it has a return statement, which returns the data of the target type or calls the constructor of the target class.

There are two main types of type 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;
}

Results:


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309121016419.jpg ">

You can see that the operator int() const function is implicitly called when the add(d1,d2) function is called.

2) conversion of objects to 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;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013091210164210.jpg ">

The X class in the above program converts an x-type object to type A by using the "operator A()" type conversion.

2. Operator is used for operator overloading:

The concept of operator overloading:
Associate an existing operator with a member function and use that operator with its member object (operand).

Notes:

1) overloading cannot change the basic functionality of an operator, as well as the priority order of the operator.

2) overloading should not change the original meaning of the operator.

3) you can only overload existing operators, not new symbols.

4) operator overloading is only available for classes.

5) the following operators cannot be overloaded:

. Origin operator (member accessor)

* pointer to a member

:: scope resolver

? : question mark conditional operator

The number of bytes of sizeof operand

General format of operator functions:

Return_type operator op (argument list);

Return_type: return type (what to get)

Op: the operator to override

Argument list: argument list (what are the operands)


Related articles: