Topic: operator void*() and operator int()
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1998/06/15 Raw View
>I observe that if I make a class members operator int() or operator void*(),
>I am not allowed to include a return type, although I do return a value in
>the function itself. What is the rational for this syntax?
First of all, type conversion operators are similar to constructors:
X::operator int() const;
is similar to
int::int(const X&)
The reason why you don't specify return types on constructors is so
that you can create unnamed objects, like this:
return Date(01,JAN,2000).wday(); // returns SAT
If Date::Date(...) returns something, like an error code, you can't
do the above line.
Also, returning unnamed objects avoids a call to the copy constructor
and destructor, and hence results in faster code (although an optimizer
can avoid the extra calls):
Date d(01,JAN,2000); // calls Date::Date(...)
return d; // calls Date::Date(const Date&) and Date::~Date()
On the other hand,
return Date(01,JAN,2000); // calls Date::Date(...)
Finally, it seems dangerous to define two operator conversion
functions.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Charles Miller" <cmiller@oasystel.com>
Date: 1998/06/05 Raw View
I observe that if I make a class members operator int() or operator void*(),
I am not allowed to include a return type, although I do return a value in
the function itself. What is the rational for this syntax?
Eg:
class X
{
public:
operator int();
operator void*()
};
X::operator void*() { return 0; } // Note the lack of a return type
in the signatures
X::operator int() { return 1; }
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/06/05 Raw View
"Charles Miller" <cmiller@oasystel.com> writes:
>I observe that if I make a class members operator int() or operator void*(),
>I am not allowed to include a return type, although I do return a value in
>the function itself. What is the rational for this syntax?
Constructors, destructors, and type-coversion functions have
special rules. A rule common to all three is that you are
not allowed to declare a return type for them. I would say
the rationale is that it would be an unhelpful redundundancy.
In particular, the name of a type conversion function is also
its return type; requiring that you specify the return type
(again) is not a helpful form of reduncancy.
At any rate, the C++ rule is that you are not allowed to
declare a return type for constructors, destructors, or
type-conversion functions.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]