问题描述
是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
推荐答案
这是完全可以接受的。
只是不要尝试与建造者一起做,它不会实现你想要的。
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...
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
是的,但更喜欢:
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
这篇关于重载运算符(基本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!