[comp.lang.c++] Question about type conversion.

dent@hpdtczb.HP.COM (Steve Dent) (04/30/91)

Sorry If this is question has been asked before.

I'm developing a program which using existing C routines.  One of the old C 
routines returns a variable which may have multiple types: i.e an integer,
a double, or a pointer to a character.  The user of this routine is 
provide conversion macros to convert the variable to a known type.  The 
definition of the return structure is as follows:

struct FIELD
{
   int type;
   void *fieldValue;
}


A conversion macro would look like this:

#define convInt(a) (*(int *) a->fieldValue)

What I would like to do is create a C++ wrapper around this structure which
will take care of conversion for the user.  So instead of:

	var1 = convInt(ret1);

It would look like this:
	
	var1 = ret1;

I tried to create an overloaded operator= function as follows:

inline
void operator=(int tmp, FIELD *val)
{
    tmp = (*(int *) val->fieldValue);
}

NOTE:  this is simplified for an example.

The problem is that the ATT 2.1 compiler gives me the following warning:

CC: "s1.C", line 87: warning: non-member operator =() (anachronism) (1531)

Which after looking up means that overloading the operator=() will only be 
supported as a member functions in future releases of the C++ language.  
I'm really trying to archive is type conversion not an assignment. 

The real question is how do I inform the C++ compiler of type conversion rule
from a user defined class to a built in type?  Is this possible?

Steve Dent
Design Technology Center
Hewlett Packard

dent@hpdtczb.HP.COM (Steve Dent) (05/01/91)

Never mind, I solved the problem.  I didn't realize that there are
conversion function member functions.  After reading about these
functions, I realized the error in my ways.

Hope I didn't that up too much band width.

Steve Dent
Design Tech Center
Hewlett Packard

bytor@ctt.bellcore.com (Ross Huitt) (05/02/91)

In article <9580001@hpdtczb.HP.COM> dent@hpdtczb.HP.COM (Steve Dent) writes:
//....

>   One of the old C 
>routines returns a variable which may have multiple types: i.e an integer,
>a double, or a pointer to a character.  The user of this routine is 
>provide conversion macros to convert the variable to a known type.  The 
>definition of the return structure is as follows:
>
>struct FIELD
>{
>   int type;
>   void *fieldValue;
>}

//....

>What I would like to do is create a C++ wrapper around this structure which
>will take care of conversion for the user.  So instead of:
>	var1 = convInt(ret1);
>It would look like this:
>	var1 = ret1;

>The real question is how do I inform the C++ compiler of type conversion rule
>from a user defined class to a built in type?  Is this possible?
>
>Steve Dent
>Design Technology Center
>Hewlett Packard

How about something like:

struct FIELD
{
   int   type;
   void *fieldValue;
   inline operator int(void)    { return *(int*)fieldValue; }
   inline operator double(void) { return *(double*)fieldValue; }
   inline operator char *(void) { return (char *)fieldValue; }
};

// this would let you do something like:

FIELD f;
int x = f;
double y = f;
char *s = f;


However, I try to avoid cast operators for my classes unless I have
a _very_ _very_ good reason for doing so.  They can be very deadly
because they may get invoked where you don't intend.  This example
looks harmless enough, though. :-)

-bytor