Overloading the typecast operator

N

Nimmi Srivastav

There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents a rather good
discussion of typecast operator overloading.

I am presenting below a summary of what I have gathered. I would
appreciate if someone could point out to something that is specific to
Borland C++ and is not supported by the ANSI standard. I am also
concerned that some of the information may be outdated since the book
is quite old (1991 edition).

1). Cast operator functions are declued using the following syntax:
operator typename ();
operator typename* ();

2). The target type of the conversion cannot be an enumeration or a
typedef name.

3). You rannot specify a return type.

4). You cannot declare arguments for a cast operator function. It is
assumed that
the function is dealing with *this as input.

5). Cast operator functions are inherited and they can be virtual
functions

6). Only one cast operator function can be implicitly applied to a
class object

7). Cast operator functions cannot be overloaded

8). A cast operator function in a derived class hides a cast operator
function in its base class only if the target type is exactly the
same.

9). The cast operator functions are used in serializing operations.

// ~~~~~~~ Code snippet begin ~~~~~~~
#include <iostream.h>

//////////////////////////////////////////////////////
class A
{
int dat;

public:
A(int num = 0 ) : dat(num) {}

operator int() {return dat;} // castop to int
};


//////////////////////////////////////////////////////
class X
{
int dat;

public:
X(int num = 0) : dat(num){}

operator int() {return dat;} // castop to int

operator A() // castop to class A
{
A temp = dat;
return temp;
}
};


//////////////////////////////////////////////////////
int main()
{
X stuff = 37;
A more = 0;
int hold;

hold = (int)stuff;
cout << hold << endl;

more = stuff; // convert X::stuff to A::more
hold = (int)more; // convert A::more to int
cout << hold << endl;
}


// ~~~~~~~ Code snippet end ~~~~~~~

Regards,
Nimmi
 
R

Rolf Magnus

Nimmi said:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents a rather good
discussion of typecast operator overloading.

I am presenting below a summary of what I have gathered. I would
appreciate if someone could point out to something that is specific to
Borland C++ and is not supported by the ANSI standard. I am also
concerned that some of the information may be outdated since the book
is quite old (1991 edition).

1). Cast operator functions are declued using the following syntax:
operator typename ();
operator typename* ();

They are actually not "cast operators", but "conversion operators". And
your second operator is just the same as the first, with typename being
a pointer type.
2). The target type of the conversion cannot be an enumeration or a
typedef name.

I don't see any reason for that. Should both work. Especially the
typedef is just an alias for another type. Wherever you can use the
original type, you can use the typedef too.
3). You rannot specify a return type.

Right. The return type is always the same as the "target type" and must
not be specified again.
4). You cannot declare arguments for a cast operator function.

Right. How could you call it with arguments?
It is assumed that the function is dealing with *this as input.

That's the case for any member function.
5). Cast operator functions are inherited and they can be virtual
functions
Yes.

6). Only one cast operator function can be implicitly applied to a
class object

One implicit conversion is done, be it through a conversion constructor
or a conversion oprerator.
7). Cast operator functions cannot be overloaded

Not quite true. You can have const and non-const overloads.
8). A cast operator function in a derived class hides a cast operator
function in its base class only if the target type is exactly the
same.

Yes. If it's not, it's another conversion operator.
9). The cast operator functions are used in serializing operations.

This is just one example where they might be used. I don't see that as
something that is especially connected to conversion operators.

// ~~~~~~~ Code snippet begin ~~~~~~~
#include <iostream.h>


//////////////////////////////////////////////////////
class A
{
int dat;

public:
A(int num = 0 ) : dat(num) {}

operator int() {return dat;} // castop to int
};


//////////////////////////////////////////////////////
class X
{
int dat;

public:
X(int num = 0) : dat(num){}

operator int() {return dat;} // castop to int

operator A() // castop to class A
{
A temp = dat;
return temp;
}

Whenever possible, you should use a conversion constructor in the target
class instead of a conversion operator in the source class. So instead
of that operator A() in X, you should add a constructor to A like:

A(const X& rhs)
: dat(rhs)
{}

This will directly construct the A object from the X object instead of
first default-constructing it and then changing it. Another reason is
that you can make conversion constructors explicit.
But for this to work, your operator int() in X has to be const, like:

operator int() const {return dat;}
};


//////////////////////////////////////////////////////
int main()
{
X stuff = 37;
A more = 0;
int hold;

hold = (int)stuff;

You don't need to cast here, the conversion is done implicitly. You
should also avoid C style casts and use the more fine-grained C++
casts.
cout << hold << endl;

You could just directly write this as:

cout << stuff << endl;

The conversion will be done implicitly.
more = stuff; // convert X::stuff to A::more
hold = (int)more; // convert A::more to int

You can skip that cast, too. Done implicitly.
 
J

Jonathan Turkanis

Rolf Magnus said:
Nimmi Srivastav wrote:

I don't see any reason for that. Should both work. Especially the
typedef is just an alias for another type. Wherever you can use the
original type, you can use the typedef too.

True. Also, in some cases, a typedef is absolutely necessary, e.g.
when converting to a pointer to an array:

struct Thing {
typedef int (*array) [9];
operator array(); // okay
operator int (*) [9] (); // syntax error
};

Right. How could you call it with arguments?

If they had default values. This would actually be very conveinent: it
would allow the use of the enable_if utility with conversion
operators. (CUJ June 2003)

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top