Anonymous classes

In Java, the following is commonly used (for example, to handle events) to use a template template template:

abstract class SomeAbstractClass { public abstract void SomeFunction (); } //... SomeAbstractClass obj = new SomeAbstractClass () { public void SomeFunction () { /* implementation */ } }; 

In C ++, the following compilations:

 class SomeAbstractClass { virtual void SomeFunction () = 0; }; // ... SomeAbstractClass * obj = new ( class : public SomeAbstractClass { virtual void SomeFunction () { /* implementation */ } }); 

Why don't people usually do this?

+4
source share
1 answer

Three problems that I think are encountered with an anonymous class

  • You cannot write a constructor because the class does not have a name.
  • Inheritance of the initializer list is not allowed.
  • capture value is also complicated, the final variable is only available.
+1
source

Source: https://habr.com/ru/post/1496397/


All Articles