const override and odd return statment

Hey guys, I am attempting to understand how this method works:

1
2
3
4
  QString unit() const override {
        if(m_UnitsPlugin == nullptr) return "N/A";
        return useUsCustomaryUnits() ? m_UnitsPlugin->toStr(m_Us_Customary) : m_UnitsPlugin->toStr(m_metric);
    }


The first thing I want to confirm is that since this method is const override does that mean there is another unit() method to which this method is overriding? Or does const override mean something else?

Secondly, what is happening in that return statement?

override means that the class this is a member function of inherits from a class that contains a virtual function of the same name and signature; this function here is overriding that parent virtual function.

const here is a promise that this function won't change any member variables, and won't call any functions that might.

what is happening in that return statement?

The statement
useUsCustomaryUnits() ? m_UnitsPlugin->toStr(m_Us_Customary) : m_UnitsPlugin->toStr(m_metric);
is being evaluated, and the value of that statement returned.

It's roughly the same as

1
2
if ( useUsCustomaryUnits() == true)  {return  m_UnitsPlugin->toStr(m_Us_Customary)}
                               else  {return m_UnitsPlugin->toStr(m_metric);}
Last edited on
@Repeater Interesting, thanks for your help! I tried deleting the 'override' keyword and compiling the program in an attempt to find the method this method is overriding but the program compiled just fine without it?
The override keyword is for safety and maintainability (readability). If you try to add the "override" keyword to a method that you aren't actually overriding, you will get a compiler error. This is important in case a programmer ever makes a typo or breaking change to the interface; without the keyword, the compiler will still happily define that new function, but no logic will ever call it (via polymorphism).

______________________________

The practical reason why it isn't mandatory is that it was only added as a standard feature in C++11.
Last edited on
Yes, it will.

When you use the keyword "override", it is a signal to the compiler (and the next person to read the code) that you intend this to be overriding something. If it's NOT doing that, then the compiler will tell you.

However, if you override a virtual function with explictly marking it "override", that's fine. The compiler only checks one way. It does not check that every time you override something, you said "override".
Last edited on
does that mean there is another unit() method


Yes, but there's a slight catch.

There can be a virtual "unit" in the base, which does something else (usually more general, where you're providing something more specific for the situation appropriate for the class you're working on).

On the other hand, "unit" could be a pure virtual function in the base. If so, there's no other "unit" method, at least not one with a body to it. In this case the pure virtual declares a function all derived classes are required to define, and is an acknowledgement of the author of the base class that there can't be default (or more general) behavior, putting the demand on the derived class to provide it.
Default implementation there can be:
https://en.cppreference.com/book/intro/abstract_classes
Topic archived. No new replies allowed.