S
subramanian100in
In the book, C++ Coding Standards book by Hereb Sutter and Andrei
Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit
conversions", the authors have advised the use of named functions that
offer conversions instead of conversion operators.
In page 87, example 2: Errors that work.
class String
{
// ...
public:
operator cons char *(); // deplorable form
};
Assume s1, s2 are 'String's:
int x = s1 -s2; // compiles; undefined behaviour
const char *p = s1 - 5; // compiles; undefined behaviour
....
I am NOT going against the authors in asking the following.
However, as a beginner I just to want to know if conversion operators
should be avoided totally ?
Because, in <istream>, we have operator bool() const; (in the sentry
class)
This helps in
while(cin)
// ...
So here the standard library uses a conversion operator. Are there any
guidelines as to when the conversion operators can be defined ?
Kindly clarify.
I once again state that I am not confronting the authors. I am asking
this question from learner's point of view.
Thanks
V.Subramanian
Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit
conversions", the authors have advised the use of named functions that
offer conversions instead of conversion operators.
In page 87, example 2: Errors that work.
class String
{
// ...
public:
operator cons char *(); // deplorable form
};
Assume s1, s2 are 'String's:
int x = s1 -s2; // compiles; undefined behaviour
const char *p = s1 - 5; // compiles; undefined behaviour
....
I am NOT going against the authors in asking the following.
However, as a beginner I just to want to know if conversion operators
should be avoided totally ?
Because, in <istream>, we have operator bool() const; (in the sentry
class)
This helps in
while(cin)
// ...
So here the standard library uses a conversion operator. Are there any
guidelines as to when the conversion operators can be defined ?
Kindly clarify.
I once again state that I am not confronting the authors. I am asking
this question from learner's point of view.
Thanks
V.Subramanian