6

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?

4
  • 1
    I don't think that is possible. But if the final goal is to use the new subclass in Design mode of Qt Creator, then it is perfectly possible. Commented Aug 28, 2014 at 7:39
  • Why don't you want to subclass? Commented Aug 28, 2014 at 7:44
  • 1
    Basically because i would have to subclass like 10 different classes and its only one function of each class which i need to reimplement. so i would save some time. But as Ali said, i also dont think it is possible. Commented Aug 28, 2014 at 7:55
  • In Principle you can do this only via assembler, but this is likely not what you are looking for and a rather hackish way. In the context of C++ it is not possible. Commented Aug 28, 2014 at 8:17

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

Comments

7

Your Java example is a subclass -- it's just an anonymous subclass. The @Override keyword is simply a diagnostic aid: it issues an error if the method doesn't override a superclass. Removing @Override has no effect on the generated code. C++11 has it too -- see this link.

In C++ as in Java, you can't override a virtual function without declaring a subclass. If you want to use Qt effectively, you will have to get used to it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.