You cannot override without inheritance. The code in the related example makes a subclass. The confusion is probably due to the fact that it does not use the extends . It creates an anonymous subclass of XStream and overrides it. Such classes exist in C ++, and similar code is possible. The naming convention is slightly different. Classes that do not have a name but have a named instance are called unnamed â . Here is my code transliteration to show how an example can be executed with an unnamed class in C ++:
class SomeClass { public: void myMethod() { class: public XStream { protected: MapperWrapper wrapMapper(const MapperWrapper& next) override { return MapperWrapper(next);
You can replace XStream with QWidget and wrapMapper with one of its virtual classes if you want to override it this way.
Anonymous classes are often used for callbacks in Java. But in C ++ we have pointers to functions and lambda lately, which is probably why using unnamed classes is much less common in C ++ code compared to Java. Also, prior to C ++ 11, unnamed classes were not allowed as template parameters, so they would be a poor choice for a callback function.
â In C ++, an anonymous class (or struct) will be the same that does not have a named instance. It can be a member of another outer class, and members of the anonymous class will be migrated to the namespace of the parent class. In addition, anonymous classes are not allowed by the standard. How then can there be a definition for such a thing? Well, anonymous unions are allowed, and anonymous classes are similar to them. However, anonymous structures are allowed by the C11 standard.
source share