Why can't we define an anonymous class for inheritance?

class MyClass : SomeFeatureGeneratedByTemplate<MyClass>

The template offers a lot of convenience for adding functions to our class, simply inheriting the template of the class instance.

However, sometimes this function may be too complex to implement as a template, where a macro may be the only choice.

    MACRO_TO_GENERATE_COMPLICATED_FEATURE(MyClass) 
   /* Might be expanded to 
    #ifndef MYCLASS_FEATURE_CLASS
    #define MYCLASS_FEATURE_CLASS
        class MyClassFeature { ... };
    #endif
   */
   class MyClass : MyClassFeature 

I wonder if the following syntax will simplify: let you define an anonymous class in place

class MyClass : class { ... }, class{ ... }

Therefore, the above code can be rewritten as:

class MyClass : MACRO_GEN_FEATURE(MyClass)

APPEND:

Q: Why I do not embed the code only inside the class?

A: 1. This function must be explicit and accessible to the user. When they generate documents, the derived class is easy to detect: class A: FEATURE1(A), FEATURE2(A)but the inline macro does not. Although an empty pool can be obtained to achieve our goal (e.g. class A: FEATURE1(A)//just derive predefined struct FEATURE1_EMPTY{};), this is apparently not an ideal solution.

  1. - , , (, static_assert ).

  2. , , , 2).

, , ++?

+4
2

( 1980- ), - (1990- , ) . .

:

  • ? !

:

  • ? !
+5
class MyClass : class { /* CONTENT */ } {
  // HERE
};

, CONTENT, MyClass, , HERE.

"" , "" :

class Thing {
  class SomeSubThing {} subthing;
};

, , .

+5

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


All Articles