我是一个新的C ++初学者,我怎样才能使用overload()运算符,以及如何使用它以及在什么情况下使用它 [英] I am a new C++ beginner, how can I overload () operator, and how to use it and in what situation

查看:84
本文介绍了我是一个新的C ++初学者,我怎样才能使用overload()运算符,以及如何使用它以及在什么情况下使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用:

当我读到我的C ++书籍时,我无法清楚地理解为什么operator()应该被重载,你怎么看?关于operator()?

When I read my C++ book,I can't clearly understand why operator () should be overloaded,how do you think about operator ()?





我尝试过:



当我阅读我的C ++书籍时,我无法清楚地理解为什么应该重载operator(),你如何看待operator()?



What I have tried:

When I read my C++ book,I can't clearly understand why operator () should be overloaded,how do you think about operator ()?

推荐答案

您试图重载函数调用操作符()。它通常用于在函数调用中为可变数量的参数创建支持。



示例 [ ^ ] 。
Your trying to overload the function call operator, "()". It's normally used to create support for a variable number of arguments in a function call.

Example[^].


首先,这是高级C ++功能之一。如果你是初学者,如果你对C ++的其余部分感到满意,我建议你自己检查一下。如果没有,你可以安全地将操作员重载后期推迟到以后:该功能是先进的,对任何事情都不重要。



你应该明白:有没有与使用重载运算符相关的技术或代码设计问题,包括()。使用一个或另一个重载的语法只有一个问题,这可以通过遵循C ++参考手册简单地解决。您需要了解:运算符重载功能只是 语法糖 。这不是问题或要求,只是可能性。



在这方面,问题为什么运营商应该超载不太正确。它不应该,它可以。我知道你正在寻找它背后的一些理由。但是没有太多的理由(不像,比如,深入的技术方面,比如虚拟机制,动态调度),除了你自己的想法,你如何使用你制作一些复杂,优雅和紧凑的C ++ API。让我们假设,你真正的问题是(我的意思是,会是,我不确定你的实际动机):如果我们可以在没有它的情况下做任何事情,我们为什么要使用()运算符重载。然后有效的答案是:你是对的,为什么呢?因为你真的不需要它,不要使用它或为什么不呢?。



请参阅,例如,函数调用operator()在C ++中重载



如您所见,此处的()运算符调用模仿构造函数调用。您还可以使用静态函数返回声明类的实例,但它需要一个单独的名称。如果创建实例的方法需要不止一次签名(通常称为工厂方法),那么您将拥有多个命名函数,可能会重载,但()保存您从发明额外姓名。



现在,这是给你的警告。实际上,您可以通过运算符重载使某些API更加优雅。但是混淆代码用户要容易得多。我记得在Bjarne Stroustrup的一些书中,关于运算符重载的部分中的大部分内容都是针对野外运算符重载使用的警告。对于用户来说,一种调用函数的新方法(上面引用的文章中的表达式),通常的C ++语法总是最自然和清晰的。



该方法应该来自您要创建的某个类接口的功能。您可能会想:我可以改进C ++代码的使用吗?我可以使用这个或那个,重载[]或()吗?啊哈!这是一个想法...



-SA
First of all, this is one of the advanced C++ features. If you are a beginner, I would advice to check yourself if you are quite comfortable with "the rest of C++". If not, you can safely post-pone the operator overloads to a later time: the feature is advanced and not critically important to anything.

You should understand: there are no technical or code design issues related to the use of overloaded operators, including (). There is just a problem of syntax of using one or another overload, which is simply solved by following C++ reference manual. You need to understand: the operator overloading feature is just the syntactic sugar. It is not a problem or requirement, it is just the possibility.

In this respect, the question "why the operator should be overloaded" is not quite correct. It should not, it can be. I understand that you are looking for some rationale behind it. But there is no much of rationale (unlike, say, deeply technological aspects, such as virtual mechanism, dynamic dispatch) except your own idea of how you can use to make some C++ API you produce intricate, elegant and compact. Let's assume, your real question would be (I mean it, "would be", I'm not sure about your actual motivation): "why should we use () operator overloads if we can do everything without it". Then the valid answers would be: "You are right, why indeed? as you don't really need it, don't use it" or "Why not?".

Please see, for example, Function call operator () overloading in C++.

As you can see, the () operator call here mimics the constructor call. You could also have a static function returning the instance of the declaring class, but it would require a separate name. If you need more than once signature for the methods creating instances (often called "factory methods"), you wold have several named functions, possibly "overloaded", but () "saves" you from inventing extra names.

Now, here is a warning for you. Indeed, you can have some API more elegant with operator overloading. But it's much easier to confuse the user of your code. As I remember, in some books by Bjarne Stroustrup, the big part of the sections on operator overloading is devoted to the warning against wild operator overloading use. For a user, "a new way to call a function" (the expression from the article referenced above), a usual C++ syntax is always the most natural and clear.

The approach should come from the functionality of some class interface you want to create. You may think: can I improve the usage of my C++ code? Can I use this or that, overload [] or ()? Aha! Here is the idea…

—SA


这篇关于我是一个新的C ++初学者,我怎样才能使用overload()运算符,以及如何使用它以及在什么情况下使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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