Explicit Conversion Operators
Explore the use of explicit conversion operators in C++ since C++11 to manage and restrict implicit conversions between user-defined types. Understand the differences from C++98, how to apply the explicit keyword with constructors and conversion operators, and how this helps prevent unintended conversions in your classes.
We'll cover the following...
We'll cover the following...
Asymmetry in C++98 #
In C++98, the explicit keyword was only supported for conversion constructors. Conversion operators converted user-defined objects implicitly.
All this changed in C++11. Now, we can overload conversion operators to explicitly prevent and permit conversions.
Let’s suppose that a class called MyClass can perform conversions from class A to MyClass ...