Is there any way to disable the conversion operator of a class? Assume it is a library class and I cannot modify the source code (or headers). I sometimes encounter a library that thinks to be clever and defines conversions which are silly and sometimes just dangerous.
For example, given this declaration in a header which I cannot modify:
class TooClever
{
...
public:
operator char const*();
};
Is there any way (trickery allowed, even if compiler specific) I can prevent this operator from ever being used in my code?
A conversion operator, in C#, is an operator that is used to declare a conversion on a user-defined type so that an object of that type can be converted to or from another user-defined type or basic type. The two different types of user-defined conversions include implicit and explicit conversions.
Conversion Operators in C++ C++ supports object oriented design. So we can create classes of some real world objects as concrete types. Sometimes we need to convert some concrete type objects to some other type objects or some primitive datatypes. To make this conversion we can use conversion operator.
Conversion function syntaxConversion functions have no arguments, and the return type is implicitly the conversion type. Conversion functions can be inherited. You can have virtual conversion functions but not static ones. Parent topic: User-defined conversions (C++ only)
There are two types of user-defined conversions: Conversion constructors and conversion functions.
Create your own descendant, add operator char const*()
to it, but make it private. While still present, this ensures it can't be called accidentally (in fact, at all).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With