C + + lesson 42 -- type conversion function, converting the current type to another type -- type conversion function

Posted by RagingEagle on Mon, 03 Feb 2020 09:33:41 +0100

This paper studies the C + + course of Mr. Tang zuolin from Ditai Software Institute

Experiment 1: type conversion function: convert the current class to another class, implicit type conversion
Experiment 2: mutual conversion between class types -- type conversion inner function VS conversion constructor
Experiment 3: avoid conflict between type conversion function and conversion constructor
Experiment 4: replace type conversion function with ordinary member function of Type toType() class

Experiment 1: type conversion function: convert the current class to another class, implicit type conversion

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int mValue;
public:
    Test(int i = 0)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
    operator int ()
    {
        return mValue;
    }
};

int main()
{   
    Test t(100);
    int i = t;// --->     int i = t.operator int()
    
    cout << "t.value() = " << t.value() << endl;
    cout << "i = " << i << endl;
    
    return 0;
}

	
mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 42-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
t.value() = 100
i = 100
mhr@ubuntu:~/work/c++$ 

Experiment 2: mutual conversion between class types -- type conversion inner function VS conversion constructor

#include <iostream>
#include <string>

using namespace std;

class Test;

class Value
{
public:
    Value()
    {
    }
    //Conversion constructor 
    explicit Value(Test& t)
    {
    }
};

class Test
{
    int mValue;
public:
    Test(int i = 0)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
    //Type conversion function
    operator Value()
    {
        Value ret;
        cout << "operator Value()" << endl;
        return ret;
    }
};

int main()
{   
    Test t(100);
    Value v = t;
    
    return 0;
}


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 42-2.cpp
42-2.cpp: In function 'int main()':
42-2.cpp:42:15: error: conversion from 'Test' to 'Value' is ambiguous
     Value v = t;
               ^
42-2.cpp:31:5: note: candidate: Test::operator Value()
     operator Value()
     ^
42-2.cpp:14:5: note: candidate: Value::Value(Test&)
     Value(Test& t)
     ^
mhr@ubuntu:~/work/c++$ 

When conversion constructors and type conversion functions appear at the same time, when compiling to Value v = t; this line of code, the compiler has a problem. He does not know which function to call for type conversion, and will report an error.

Conversion constructors and type conversion functions are inverse to each other, but if they are in the process of conversion between class types, they may have conflicts. As a result, the compiler will report an error if it does not know which function to call for type conversion. In engineering, we usually add the explicit keyword before the conversion constructor to avoid the compiler implicitly calling the conversion constructor for type conversion, so the compiler chooses the type conversion function for type conversion.

Experiment 3: avoid conflicts between type conversion functions and conversion constructors

#include <iostream>
#include <string>

using namespace std;

class Test;

class Value
{
public:
    Value()
    {
    }

	//Avoid implicit calls
    explicit Value(Test& t)
    {
    }
};

class Test
{
    int mValue;
public:
    Test(int i = 0)
    {
        mValue = i;
    }
    int value()
    {
        return mValue;
    }
    operator Value()
    {
        Value ret;
        cout << "operator Value()" << endl;
        return ret;
    }
};

int main()
{   
    Test t(100);
    Value v = t;//Only type conversion function can be selected for type conversion
    
    return 0;
}

mhr@ubuntu:~/work/c++$ g++ 42-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
operator Value()
mhr@ubuntu:~/work/c++$ 

The implicit call of type conversion function cannot be suppressed in the program (the conversion constructor can be suppressed), so the type conversion function is usually not defined in the code, but implemented in the class as a common member function of toType().


223 original articles published, 100 praised, 80000 visitors+
Private letter follow

Topics: Ubuntu