Type conversion question

Hi all,

i was wondering if any of you could tell me how to define(I.E. Correct syntax) a type conversion for a operator= for types i can not edit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  
//Cant edit this Type (in some lib)
struct Color
{
	unsigned __int8 R = 3, G = 3, B = 3, A = 3;
};

//Cant edit this Type (in some lib)
class Colour
{
public:
	int R = 1, G = 1, B = 1, A = 1;
};

Colour operator=(Colour& Colour, const Color& B) //<-- error C2801: 'operator =' must be a non-static member
{

}

int main()
{
	Color One;

	Colour Two;

	Colour three = One; //<-- i would like to use the = ,error C2440: 'initializing' : cannot convert from 'Color' to 'Colour'

	return 0;
}
Last edited on
Define conversion operator:
1
2
3
4
5
6
7
8
9
10
class Colour
{
public:
	/*...*/
    operator Color()
    {
        Color a = /*initialize Color here*/;
        return a;
    }
};

And C++ has standard type called uint8_t. __int8 is non-standard

EDIT: Aparently I can't read: i read comment to second class as "Can edit this Type"
Last edited on
> a type conversion for a operator= for types i can not edit.

operator=, conversion constructors and cast operators have to be members.

The best we can do if the types can't be touched is write a pair of free functions:

1
2
Colour& assign( Colour& dest, Color srce ) ;
Color& assign( Color& dest, Colour srce ) ;


(Or provide for implicit conversions in derived class(es) )
Last edited on
Hmm I was afraid it was not possible.... although I thought I saw someone do it in there code. I will just fix it by making a conversion function. Thanks for the help anyways.
Topic archived. No new replies allowed.