Return the wrong type in C ++

I have a Color class that contains three components of float (r, g, b).

I need to program the following function:

Color getColor (unsigned char values โ€‹โ€‹[], int i)

Usually I should program it like this:

Color getColor(unsigned char values[], int i){ return Color((float) values[i]/255.0, (float) values[i+1]/255.0, (float) values[i+2]/255.0); } 

But by mistake I did

 return values[i]; 

When I compiled, I did not receive any compilation error, and I did not receive a runtime error either.

Why is this possible?

+6
source share
2 answers

This may be the result of an implicit constructor of the Color class that takes an unsigned char as an argument.

This means that you have a constructor in the Color class with a single argument, or the remaining parameters remain the default parameters.

+5
source

Test your constructor in the Color class and overwrite it.

0
source

Source: https://habr.com/ru/post/978325/


All Articles