Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt/C++ Override function without subclassing

I would like to override a virtual function of a QWidget without subclassing it. It is possible in java. I found this link:

overriding methods without subclassing in Java

Not sure if there is a way in c++ too. Any ideas?

like image 635
Cocomico Avatar asked Aug 28 '14 07:08

Cocomico


People also ask

Can you override non-virtual methods?

The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. By default, methods are non-virtual. You cannot override a non-virtual method.

Can you override function in C?

There is no function overriding in C, only in C++.

Can we override non-virtual method in Java?

Yes, you can override a non-virtual function in a derived class.

What is override in C ++ 11?

C++11 adds two inheritance control keywords: override and final. override ensures that an overriding virtual function declared in a derived class has the same signature as that of the base class. final blocks further derivation of a class and further overriding of a virtual function.


1 Answers

You can't override without inheritance. The code in the linked example does subclass. Perhaps the confusion comes from the fact that it doesn't use the extends keyword. It creates an anonymous subclass of XStream and overrides it's method. Such classes exist in C++ as well and similar code is possible. The naming convention is a bit different. Classes that have no name, but do have a named instance are called unnamed . Here is my transliteration of the code to show how the example can be done with unnamed class in C++:

class SomeClass {
public:
    void myMethod() {
        class: public XStream {
        protected:
            MapperWrapper wrapMapper(const MapperWrapper& next) override {
                return MapperWrapper(next); // the example is cut off here, persumably it's creating another nested anonymous class, but i'll keep this simple
            }
        } xstream;
    }
};

You can replace the XStream with QWidget and wrapMapper with one of it's virtual classes if you want to override it in this way.

Anonymous classes are often used for callbacks in Java. But in C++ we have function pointers and lately lambdas, which is probably why the use of unnamed classes is much rarer in C++ code compared to Java. Also, before c++11 unnamed classes were not allowed as template parameters, so they would have been a poor choice for a callback functor.

In c++, an Anonymous class (or struct) would be one that has no named instance either. It could be a member of another, outer class and the members of the anonymous class would be brought to the namespace of the parent class. Except, anonymous classes are not allowed by the standard. How can there be a definition for such thing then? Well, anonymous unions are allowed and anonymous classes are analoguous to them. Anonymous structs are allowed by C11 standard, though.

like image 192
eerorika Avatar answered Sep 30 '22 02:09

eerorika