c++11 - Extension methods in c++ -


I was searching for the implementation of extension methods in c ++ and it is mentioned that polymorphic_map < / Code> can be used for methods associated with a class, but, provided the link is considered dead, does anyone know what its answer was referencing, or if in a way Extension methods (possibly through some use of a mixine) Other way?

I know that canonical C ++ solution is to use free functions; It's more of curiosity than anything else.

Development of different languages ​​in different ways, especially in C # and Java There is an approach of strong perspective in relation to the OO, due to which everything is something mentality (C # is a bit diminished). In that approach, extension methods provide an easy way to add an existing object or interface to new features.

There are no extension methods in C ++, nor are they required when developing C ++, everything forgets an object pattern - by the way, Java / C # [*] is also wrong. A different mindset is taken in C ++, there are objects, and objects are operations that are naturally part of the object, but there are other operations that are part of the interface and not being part of the class. Required. A should read by Herb Sutter, where the author's defense (and I agree) that you can easily increase any class with free functions.

As a specific simple example, there are some member methods to dump some primitive type of content into standard templated class original asream , and then it (also templated) Increased with free work that enhances the existing type of functionality using the existing public interface. For example, std :: cout & lt; & Lt; 1; is applied as a member function, while std :: cout & lt; & Lt; "Oh"; A free function is implemented in the context of other basic members.

Extensibility is achieved through free functions in C ++, not by the way of adding new methods to the existing object.

[*] One object is not .

In a given domain, there will be a set of real objects that can be modeled and applied to those operations that may occur, in some cases they will be part of the operation object, but in some other cases They will not, especially you will find utility classes in those languages ​​which claim that everything is an object and those utility classes are nothing but hide this fact Of Trying is a layer that does not belong to any particular object '

Even some tasks that are implemented as member functions, are not actually operation on objects . For an complex code class, consider the additional one for more of an operation on the first argument, plus sum (or + ) A.sum (b); Why should not or b.sum (a) sum (a, b) ?

Forcing the operation as member methods actually produces a weird effect - but we use them only: a.equals (b); and b.equals (A); There may be completely different results in even if the implementation of equal is completely symmetric (when any a or b Is a zero indicator, then what happens)

Comments