Search code examples
c++qtassignment-operator

Explicit operator= call (T::operator=)


I am reading qt sources and I've seen a code like this many times:

buttonOpt.QStyleOption::operator=(*opt);

So, I guess it is something like buttonOpt = *opt but why do they use this syntax instead of default and user-friendly? Is this faster or any other profit exists?


Solution

  • This is because they are explicitly calling the operator= from the base class of buttonOpt, which is QStyleOption.

    buttonOpt.QStyleOption::operator=(*opt);
    
    //similar behavior
    class Base
    {
    public:
        virtual bool operator<(Base & other)
        {
            std::cout << "Base";
        }
    };
    
    class Derived : public Base
    {
    public:
        bool operator<(Base & other) override
        {
            std::cout << "Derived";
        }
    };
    
    int main()
    {
        Derived a;
        Derived b;
        a < b; //prints "Derived"
        a.Base::operator <(b); //prints "Base"
    }