When to use the declaration?

I know below using C1::fn; will cite fn (...) functions declared in C1-C2, but I want to know what works best for this using in design?

If the fn () functions do not use state C1, should I declare that the helper class is better? If fn functions use state C1, does using interrupt encapsulation?

I appreciate if you can even mention some usage examples in C ++ 11. How to use the constructor using Base::Base; instead of calling it from the derived member initializer?

 class C1 { //... public: int fn(int j) { ... } double fn(double w) { ... } void fn(const char * s) { ... } }; class C2 : public C1 { //... public: //... using C1::fn; double fn(double) { ... }; }; 
+4
source share
2 answers

If fn functions use state C1, use discontinuous encapsulation?

This using statement does not break encapsulation; it does not return any private state from C1 or does not prevent C1, preserving its invariants. This is just a convenient way to expose any other fn elements of C1 has - in this case int fn(int) - so they can be taken into account when resolving calls. Think of it as functionally equivalent ...

 class C2 : public C1 { ... inline int fn(int j) { return C1::fn(j); } }; 

... but better, because you do not need to manually add and remove functions to synchronize with the C1 overload list.

If the fn () functions do not use state C1, should you declare that the helper class is the best way?

If they do not use state C1, they must be static or non-member. In my opinion, helper classes are an ugly idea. Namespaces are a common method of grouping functionality in C ++. I am fully aware of Professor Lakos’s recommendations for using classes and understanding all arguments and still disagree. I am happy to miss it if the mention of auxiliary classes was something more important for you.

+6
source

I would say that you should use using whenever necessary. That is, every time a member function of a base class is hidden by one truncated in the derived class.

My ratioanle is that the code that accesses C2 (for example, via C2 *pc ) knows that C2 derived from C1 and therefore expects to be able to call C1 functions through pc .

+1
source

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


All Articles