扩展方法在c ++中 [英] Extension methods in c++

查看:95
本文介绍了扩展方法在c ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中寻找扩展方法的实现,来自这个comp。 std.c ++讨论,其中提到 polymorphic_map 可以用于关联方法与一个类,但是,提供的链接似乎已经死了。有没有人知道答案是指什么,或者如果有另一种方式扩展类,以类似的方式扩展方法(可能通过一些使用mixins?)。

I was searching for an implementation of extension methods in c++ and came upon this comp.std.c++ discussion which mentions that polymorphic_map can be used to associated methods with a class, but, the provided link seems to be dead. Does anyone know what that answer was referring to, or if there is another way to extend classes in a similar manner to extension methods (perhaps through some usage of mixins?).

我知道规范的C ++解决方案是使用自由函数;

I know the canonical C++ solution is to use free functions; this is more out of curiosity than anything else.

推荐答案

不同的语言以不同的方式处理开发。特别是C#和Java对于OO有一个强的观点,导致一切都是一个对象心态(C#在这里有点更宽松)。在这种方法中,扩展方法提供了一种扩展现有对象或接口以添加新特征的简单方法。

Different languages approach development in different ways. In particular C# and Java have a strong point of view with respect to OO that leads to everything is an object mindset (C# is a little more lax here). In that approach, extension methods provide a simple way of extending an existing object or interface to add new features.

在C ++中没有扩展方法,也不需要。当开发C ++时,忘记一切都是一个对象范例 - 顺便说一句,即使在Java / C# [*] 中也是false。在C ++中采用了不同的思维方式,有对象,并且对象具有固有地是对象的一部分的操作,但是还有其他操作形成接口的一部分,并且不需要是类的一部分。 Herb Sutter必须阅读的是课程内容?,作者辩护(我同意),您

There are no extension methods in C++, nor are they needed. When developing C++, forget the everything is an object paradigm --which, by the way, is false even in Java/C# [*]. A different mindset is taken in C++, there are objects, and the objects have operations that are inherently part of the object, but there are also other operations that form part of the interface and need not be part of the class. A must read by Herb Sutter is What's In a Class?, where the author defends (and I agree) that you can easily extend any given class with simple free functions.

作为一个特别简单的例子,标准模板类 basic_ostream 有一些成员方法来转储一些基本类型的内容,然后使用(也是模板化的)自由函数增强,通过使用现有的公共接口将该功能扩展到其他类型。例如, std :: cout<< 1; 被实现为成员函数,而 std :: cout <

As a particular simple example, the standard templated class basic_ostream has a few member methods to dump the contents of some primitive types, and then it is enhanced with (also templated) free functions that extend that functionality to other types by using the existing public interface. For example, std::cout << 1; is implemented as a member function, while std::cout << "Hi"; is a free function implemented in terms of other more basic members.

C ++中的可扩展性通过自由函数实现,

Extensibility in C++ is achieved by means of free functions, not by ways of adding new methods to existing objects.

[*]一切都是一个对象。

[*] Everything is not an object.

在给定的域中将包含一组可以建模的实际对象和可以应用于它们的操作,在某些情况下,这些操作将是对象的一部分,但在其他一些情况下,不。特别是,你会发现实用程序类在声称一切都是一个对象的语言中,那些实用程序类只是一个试图隐藏这样的事实, t属于任何特定对象。

In a given domain will contain a set of actual objects that can be modeled and operations that can be applied to them, in some cases those operations will be part of the object, but in some other cases they will not. In particular you will find utility classes in the languages that claim that everything is an object and those utility classes are nothing but a layer trying to hide the fact that those methods don't belong to any particular object.

即使某些作为成员函数实现的操作不是对对象的真正操作。考虑添加复杂数字类,如何 sum (或 + )更多的对第一个参数的操作比第二个?为什么 a.sum(b); b.sum(a),应该不是 sum(a,b)

Even some operations that are implemented as member functions are not really operations on the object. Consider addition for a Complex number class, how is sum (or +) more of an operation on the first argument than the second? Why a.sum(b); or b.sum(a), should it not be sum( a, b )?

强制操作成为成员方法实际上产生奇怪的效果 - 但我们只是习惯他们: a.equals(b); b.equals(a); 可能有完全不同的结果,即使等于是完全对称的。 (考虑当 a b 是空指针时会发生什么)

Forcing the operations to be member methods actually produces weird effects --but we are just used to them: a.equals(b); and b.equals(a); might have completely different results even if the implementation of equals is fully symmetric. (Consider what happens when either a or b is a null pointer)

这篇关于扩展方法在c ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆