[C + +] 42 μ type conversion function

Question:Can class types be converted to normal types? #include <iostream> using namespace std; class Test { }; int main() { Test t; int i = 0; ...
Programming experiment: a preliminary study of type conversion function
Compiler behavior
Programming experiment: conversion between class types

Question:
Can class types be converted to normal types?

#include <iostream> using namespace std; class Test { }; int main() { Test t; int i = 0; i = t; return 0; }
Output: test.cpp: In function 'int main()': test.cpp:14: error: cannot convert 'Test' to 'int' in assignment
Type conversion function

  • C + + classes can define type conversion functions
  • Type conversion functions are used to convert class objects to other types

rule of grammar:

operator Type () { Type ret; // ... return ret; }

Programming experiment: a preliminary study of type conversion function

#include <iostream> using namespace std; class Test { private: 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; }
Output: t.value() = 100 i = 100

Compiler behavior

  • The compiler will try its best to make the source code pass compilation

T this object is of type Test. How can it be used to initialize variables of type int! Is it wrong now? Don't worry, I'll see if there is a type conversion function! OK, it is found that operator int() is defined in the Test class and can be converted.

  • Type conversion function

    • Same status as transform constructor
    • Enables the compiler to convert objects to other types
    • Compiler can use type conversion function implicitly

Conversion between class types? !
Type conversion function VS conversion constructor

Programming experiment: conversion between class types

#include <iostream> using namespace std; class Test; class Value { public: Value() { } explicit Value(Test& t) { } }; class Test { public: operator Value () { Value ret; cout << "operator Value ()" << endl; return ret; } }; int main() { Test t; Value v = t; // <==> Value v = t.operator Value (); return 0; }
Output: operator Value ()

Be careful:
When the Value (Test & T) {} in the Value class is not decorated with explicit, it will produce a matching conflict with the operator Value () in the test class at compile time.

error: conversion from 'Test' to 'Value' is ambiguous note: candidates are: Test::operator Value() note: Value::Value(Test&)

  • Cannot suppress implicit type conversion function calls (when defined)
  • Type conversion function may conflict with conversion constructor
  • Replacing type conversion function with public member of Type toType() in project

#include <iostream> using namespace std; class Test; class Value { public: Value() { } explicit Value(Test& t) { } }; class Test { public: Value toValue () // Pay attention here! { Value ret; cout << "operator Value ()" << endl; return ret; } }; int main() { Test t; Value v = t.toValue(); return 0; }
Output: operator Value ()

Type conversion functions are not recommended in projects. Implicit type conversion can cause unexpected problems.

Summary

  • Type conversion functions can be defined in C + + classes
  • Type conversion functions are used to convert class objects to other types
  • Type conversion functions have the same status as conversion constructors
  • Replacing type conversion function with public member function of Type toType() in Engineering

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

2 December 2019, 17:00 | Views: 9242

Add new comment

For adding a comment, please log in
or create account

0 comments