They overloaded a struct constructor?

I was checking out the "Flare Game Engine" source code for fun, and I came across this:
[line 6]
1
2
3
4
5
6
7
8
9
class Color {
public:
        Uint8 r, g, b, a;
        Color();
        Color(Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a = 255);
        operator SDL_Color() const;
        bool operator ==(const Color &other);
        bool operator !=(const Color &other);
};

Definition here:
1
2
3
4
5
6
7
8
Color::operator SDL_Color() const {
        SDL_Color c;
        c.r = r;
        c.g = g;
        c.b = b;
        c.a = a;
        return c;
}


But I couldn't find an example in their code of how this is used. I understand it's going to return the current "Color" converted to an "SDL_Color" object, but some clarification on any of these would be awesome:

What is the main purpose of declaring it as an operator?
How would it be called? (it's behind the color class scope, right? Or did the operator keyword override that?)
Or is it more like a static member function now?
How does it return a value without an explicit return type?
What is the correct term for what they are doing?

Original file found here: https://github.com/flareteam/flare-engine/blob/master/src/Utils.h
Last edited on
that SDL is a *cast operator*.
https://en.cppreference.com/w/cpp/language/cast_operator
so you will see it either in C style casts (SDL_COLOR)variable or c++ casts static_cast<SDL_Color>(variable) or something like this. It MAY also be used in assignments, or parameter passing, eg foo(variable) may be smart enough to convert it if foo takes an SDL color type, and so on.

Its an operator because that is how casting works with your objects. Casting is referred to as using a cast operation and cast operators, but people don't really think of them like that unless reading a textbook or jargoned up piece.

Cast overloads can give trouble, there are times when you might want to just use the variable with an implicit casting and the C++ won't work until it is cast into the other thing explicitly. Cout is really funky about this, in my experience. Its probably good practice, if you define this operator, to explicitly cast it wherever you use it to the type you want, if there is ANY potential at all it could cast to the other type behind your back. Others may have some tips on controlling that safely; I try not to use them very often.
Last edited on
It basically allows you to pass a Color object to any function that expects a SDL_Color as argument.
Here is one example I found:

https://github.com/flareteam/flare-engine/blob/master/src/SDLHardwareRenderDevice.cpp#L504
Image * SDLHardwareRenderDevice::renderTextToImage(FontStyle* font_style, const std::string& text, const Color& color, bool blended) {
...
    cleanup = TTF_RenderUTF8_Blended(static_cast<SDLFontStyle *>(font_style)->ttfont, text.c_str(), color);

https://wiki.libsdl.org/SDL_ttf/TTF_RenderUTF8_Blended
SDL_Surface * TTF_RenderUTF8_Blended(TTF_Font *font, const char *text, SDL_Color fg);
Last edited on
Thank you guys,
I've already got a couple of situations that I could use this for, had no idea it was available.
I'm immediately getting the google results I needed; thank you Jonnin for the terminology so I can look deeper into it and for the advisory warning as well.
Last edited on
Topic archived. No new replies allowed.