Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dangers of using an implicit string conversion operator?

Tags:

c++

What are the dangers of using an implicit string conversion operator in a custom string class?

class MyString
{
public:

   ...

   inline operator string() const { return str; }

private:
   std::string str;
};
like image 962
Marc A. Avatar asked Apr 28 '26 13:04

Marc A.


1 Answers

The main "danger" of implicit conversion is mostly: You might get an unexpected conversion.

If your string class can logically be used as a std::string, I do not think there is a problem.

like image 127
RedAgito Avatar answered Apr 30 '26 07:04

RedAgito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!