[comp.lang.c++] Overloading typecast

gessel@cs.swarthmore.edu (Daniel Mark Gessel) (06/25/91)

I have seen:
class X {
  public: 
    overload char *()
}
meaning overload the (char *) operator so that (char *)X can be meaningful.

I would like to overload the casting of a pointer to another pointer, in
user defined classes. I'm doing memory management and need to cast a pointer to
an abstract class object to a non-abstract subclass. If I can get control
of the casting operator, I can check a flag in the object itself.

Anybody know if it's available in C++ 2.0? (in particular Borland C++)
I saw it in an old C++ book. G++ seems to support it for the (char *) example
above, I just can't figure how to get it to typecast for pointers).

Any help much appreciated,

Dan

-- 
Daniel Mark Gessel
Internet: gessel@cs.swarthmore.edu

rae@alias.com (Reid Ellis) (06/28/91)

Daniel Mark Gessel <gessel@cs.swarthmore.edu> writes:
|I have seen:
|class X {
|  public: 
|    overload char *()
|}
|meaning overload the (char *) operator so that (char *)X can be meaningful.

I think you mean:

	struct X {
		operator char *()
	};

[I prefer "struct" to "class"..]

You can use "operator" methods to provide user-defined conversions
to/from user-defined and other types.

						Reid
--
Reid Ellis                                            
rae@utcs.toronto.edu        ||           rae@alias.com
CDA0610@applelink.apple.com ||  +1 416 362 9181 [work]