重载运算符(基本)

编程入门 行业动态 更新时间:2024-10-26 06:38:13
本文介绍了重载运算符(基本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是o.k. (从风格和合法性的角度来看) 函数调用自己的重载版本? 例如,我希望能够对我的对象做两件事就是 叫做节点。 我希望能够为坐标分配坐标 对应于特定的整数;我还希望根据节点的内在属性分配坐标 (独立于任何其他 特定整数。) 所以我有代码void set_coords(int x_coord,int y_coord) { } 我可以介绍代码吗? /> void set_coords() {int x; int y; x = .... .....; y = ...; set_coords(x,y); }? 如果这种事情一直都在进行,我很抱歉 - 作为一个新手, 我从来没有见过它。 如果有人想说嗯,继续尝试吧!,那么请注意 我还需要知道这是否被认为是好的从一个风格 的角度来看。使用明确的约定显然很重要 不要混淆别人。 谢谢, Paul Epstein

Is it o.k. (from the point of view of both style and legality) for functions to call on overloaded versions of themselves? For example, I want to be able to do two things to my object which is called node. I want to be able to assign coordinates to it where the coordinates correspond to specific integers; I also want coordinates assigned based on the intrinsic properties of the node (independent of any other specific integers.) So I have code void set_coords( int x_coord, int y_coord) { } Can I then introduce code void set_coords() { int x; int y; x = .........; y = ...; set_coords(x, y); } ? My apologies if this sort of thing is done all the time -- as a novice, I''ve never seen it. If someone wants to say "Well, go ahead and try it!", then please note that I also need to know whether this is considered o.k. from a style perspective. It''s obviously important to use clear conventions that don''t confuse others. Thank you, Paul Epstein

推荐答案

pa ********** @ att 写道: 没关系(从风格和合法性的角度来看)函数调用自己的重载版本? 是的。 例如,我希望能够对我的对象做两件事,即被称为节点。 我希望能够为坐标分配坐标对应于特定整数的坐标;我还希望根据节点的内在属性分配坐标(独立于任何其他特定整数。) 所以我有代码void set_coords(int x_coord,int y_coord ) { } 我可以再引入代码 void set_coords() {int x; int y; x = .........; y = ...; set_coords(x,y); }? 是的,但更喜欢: void set_coords(){ int x = / * ... * / int y = / * ... * /; set_coords(x,y); } 在分配给它们之前不需要声明x和y。 如果这种事情一直都在做,我很抱歉 - 作为一个新手,我从来没有见过它。 如果有人想说嗯,继续尝试吧!,请注意我还需要知道是否考虑到这一点好从一个风格的角度来看。使用明确的约定显然很重要,这些约定不会让别人感到困惑。 Is it o.k. (from the point of view of both style and legality) for functions to call on overloaded versions of themselves? Yes. For example, I want to be able to do two things to my object which is called node. I want to be able to assign coordinates to it where the coordinates correspond to specific integers; I also want coordinates assigned based on the intrinsic properties of the node (independent of any other specific integers.) So I have code void set_coords( int x_coord, int y_coord) { } Can I then introduce code void set_coords() { int x; int y; x = .........; y = ...; set_coords(x, y); } ? Yes, but prefer: void set_coords() { int x = /* ... */ int y = /* ... */; set_coords(x, y); } No need to declare x and y before assigning to them. My apologies if this sort of thing is done all the time -- as a novice, I''ve never seen it. If someone wants to say "Well, go ahead and try it!", then please note that I also need to know whether this is considered o.k. from a style perspective. It''s obviously important to use clear conventions that don''t confuse others.

这是完全可以接受的。 只是不要尝试与建造者一起做,它不会实现你想要的。 Ben Pope - 我不仅仅是一个数字。对很多人来说,我被称为字符串...

It''s perfectly acceptable. Just don''t try to do it with constructors, it won''t achieve what you want. Ben Pope -- I''m not just a number. To many, I''m known as a string...

pa ********** @ att 写道: 没关系(从风格和合法性的角度来看)函数调用自己的重载版本? 合法性很简单:是的,它完全合法。 例如,我希望能够对我的对象做两件事,那就是叫做节点。 我希望能够为坐标分配坐标对应于特定的整数;我还希望根据节点的内在属性分配坐标(独立于任何其他特定整数。) 很难说没有了解更多关于你的设计,但我的第一反应就是它听起来像两个概念性的不同的操作,这些操作最好用同一个名字描述。 所以我有代码void set_coords(int x_coord,int y_coord) { } 我可以介绍代码 void set_coords() {int x; int y; x = .........; y = ...; set_coords(x,y); }? 这两个set_coords重载是否是你的节点类的成员? 如果这类事情一直都在做,我很抱歉 - 作为一个新手, 我从来没有见过它。 如果有人想说嗯,继续尝试吧!,请注意我还需要知道这是否合适从一个风格的角度来看。使用明确的约定显然很重要,这些约定不会让别人感到困惑。 Is it o.k. (from the point of view of both style and legality) for functions to call on overloaded versions of themselves? Legality is easy: yes it''s perfectly legal. For example, I want to be able to do two things to my object which is called node. I want to be able to assign coordinates to it where the coordinates correspond to specific integers; I also want coordinates assigned based on the intrinsic properties of the node (independent of any other specific integers.) It''s difficult to say without knowing more about your design, but my first reaction to this is that it sounds like two conceptually different operations that are not best described by the same name. So I have code void set_coords( int x_coord, int y_coord) { } Can I then introduce code void set_coords() { int x; int y; x = .........; y = ...; set_coords(x, y); } ? Are these two set_coords overloads are members of your node class? My apologies if this sort of thing is done all the time -- as a novice, I''ve never seen it. If someone wants to say "Well, go ahead and try it!", then please note that I also need to know whether this is considered o.k. from a style perspective. It''s obviously important to use clear conventions that don''t confuse others.

Clarity比代码结构更进一步。您还需要 设计清晰度。如果你的设计具有相同的概念操作 需要以两种不同的方式调用,那么你所得到的就是 罚款。 但是如果这两个操作在概念上是不同的,那么它们应该是通过你班级的 界面中适当命名的函数提供的。他们可能都使用一些常用功能是一个 实现细节,应该对你的 类的用户隐藏。所以类似 class node_t { public: //这个曾经用过是set_coord(int,int) void move_node(int x_coord,int y_coord); //这个曾经是set_coord() void position_intrinsically(); private: void set_coords(int x_coord,int y_coord); int x_; int y_; }; void node_t :: move_node(int x_coord,int y_coord) { set_coords(x_coord,y_coord); } void node_t :: position_intrinsically() { int x = ... //无论你在set_coords()之前做了什么 int y = ... //随便你在set_coords()之前做过 set_coords(x,y); } void node_t :: set_coords(int x_coord,int y_coord) { x_ = x_coord; y_ = y_coord; } 所以,如果我有一个node_t对象,我知道我可以使用 move_node将它移到一个位置,我可以用 position_intrinsically来定位它。两个不同的操作,具有适当的名称 ,我以适当的方式调用。事实上,在内部,两个操作最终调用set_coords函数来更改节点的 坐标是一个我不在乎的实现细节 我不必看。 现在你有一个对象本质上知道在哪里定位 本身,然而允许我将它放在我选择的任何地方。只有你 才知道你的设计,由你来决定是否合理 。 Gavin Deane

Clarity extends further than the structure of your code. You also need clarity in design. If your design has the same conceptual operation that needs to be called in two different ways, then what you''ve got in fine. But if those two operations are conceptually different, they should be made available through appropriately named functions in your class''s interface. That they may both use some common functionality is an implementation detail that should be hidden from the users of your class. So something like class node_t { public: // This one used to be set_coord(int, int) void move_node(int x_coord, int y_coord); // This one used to be set_coord() void position_intrinsically(); private: void set_coords(int x_coord, int y_coord); int x_; int y_; }; void node_t::move_node(int x_coord, int y_coord) { set_coords(x_coord, y_coord); } void node_t::position_intrinsically() { int x = ... // Whatever you did in set_coords() before int y = ... // Whatever you did in set_coords() before set_coords(x, y); } void node_t::set_coords(int x_coord, int y_coord) { x_ = x_coord; y_ = y_coord; } So, if I have a node_t object, I know I can move it to a position with move_node and I can position it intrinsically with position_intrinsically. Two different operations with appropriate names that I call in the appropriate way. The fact that internally, both operations end up calling a set_coords function to change the coordinates of the node is an implementation detail I don''t care about and I don''t have to see. Now you have an object that knows intrinsically where to position itself, and yet allows me to position it anywhere I choose. Only you know your design and it''s up to you to decide whether that makes sense or not. Gavin Deane

Ben Pope< be ***************** @ gmail>写道: Ben Pope <be*****************@gmail> wrote: pa ********** @ att 写道: void set_coords() {int x; int y; x = .........; y = ...; set_coords(x,y); }? void set_coords() { int x; int y; x = .........; y = ...; set_coords(x, y); } ?

是的,但更喜欢: void set_coords(){ int x = / * ... * / int y = / * ... * /; set_coords(x,y); } 在分配给它们之前不需要声明x和y。

Yes, but prefer: void set_coords() { int x = /* ... */ int y = /* ... */; set_coords(x, y); } No need to declare x and y before assigning to them.

只要看一下这个,默认函数参数也可以做什么 OP也希望: void set_coords(int x = / * default x * /,int y = / * default y * /) { / * ... * / } - Marcus Kwok

Just looking at this, default function arguments also might do what the OP wanted too: void set_coords(int x = /* default x */, int y = /* default y */) { /* ... */ } -- Marcus Kwok

更多推荐

重载运算符(基本)

本文发布于:2023-06-03 12:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!