[comp.lang.c++] Syntax error when defining conversion operator

prc@ERBE.SE (Robert Claeson) (11/21/88)

In a class, I want to define a conversion operator from the class
to unsigned long. So, I write:

class foo {
    unsigned long bar;
public:
    ...
    operator unsigned long() { return bar; }
    ...
};

When I compile this (AT&T C++ Translator 1.1),  get a "syntax error"
from cfront. I've found NOTHING in the manual that prevents me from
defining conversion operators for multi-word data types. Is there any
way to circumvent this?
-- 
Robert Claeson
ERBE DATA AB
rclaeson@ERBE.SE

henry@utzoo.uucp (Henry Spencer) (11/26/88)

In article <337@maxim.ERBE.SE> prc@ERBE.SE (Robert Claeson) writes:
>In a class, I want to define a conversion operator from the class
>to unsigned long. So, I write:
>...
>    operator unsigned long() { return bar; }
>    ...
>When I compile this (AT&T C++ Translator 1.1),  get a "syntax error"
>from cfront. I've found NOTHING in the manual that prevents me from
>defining conversion operators for multi-word data types. Is there any
>way to circumvent this?

You're going to have to use a typedef.  If you look at the C++ Reference
Manual (in the back of The Book), you'll find that the name of a conversion
operator must be a "simple type name", and if you look up the definition
of that, you'll find that it essentially means "one word".  So you'll
need to use a typedef to give "unsigned long" a one-word name.
-- 
Sendmail is a bug,             |     Henry Spencer at U of Toronto Zoology
not a feature.                 | uunet!attcan!utzoo!henry henry@zoo.toronto.edu