jeudi 3 septembre 2020

C++ how to overload a postfix operator? [duplicate]

How to properly make a prefix and postfix operators for any type?

For example, I have this enum class

enum class Traffic_light { green, yellow, red };

I try to create the operator overloads like

Traffic_light operator++(int)
{
    return Traffic_light::green;
}


Traffic_light operator++()
{
    return Traffic_light::red;
}

But This does not work.

Traffic_light tl = Traffic_light::green;
++tl; //error
tl++; //error

error: no match for ‘operator++’

How does one overload operators in general? I expect a general guide for any type.

Aucun commentaire:

Enregistrer un commentaire