拒绝负数 [英] Rejecting negative number

查看:100
本文介绍了拒绝负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参加以下课程:


class Finger

{

public:

双倍长度;

}

我正在编写可重复使用的代码,作为独裁者,我要求长度

不能为负数。以下是完美的:


无符号双倍长度;


但是当然没有编译。

所以...我已经提出了3种可能的解决方案:A1 A2 B。

A)创建2个独立的成员函数,GetLength和SetLength。 SetLength

将拒绝负数。当提供负数时,它将:

A1)抛出异常

A2)返回错误的布尔值,表示失败


)如果长度为

设置为负数,请记录代码将具有未定义的行为。


您会建议哪个?还有其他建议吗?

我自己倾向于B.

谢谢

-JKop

Take a class like the following:

class Finger
{
public:

double length;
}
I''m writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn''t compile.
So... I''ve come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will:
A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop

推荐答案



" JKop" < NU ** @ NULL.NULL>在消息中写道

news:_u *************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:_u***************@news.indigo.ie...
参加类似课程以下内容:

班级手指
{
公开:

双倍长度;
}

我'我正在编写可重复使用的代码,作为独裁者,我要求长度不能是否定的。以下是完美的:

无符号双倍长度;

但是当然没有编译。

所以......我已经提出3种可能的解决方案:A1 A2 B.

A)创建2个独立的成员函数GetLength和SetLength。 SetLength
将拒绝负数。当提供负数时,
将:A1)抛出异常
A2)返回表示失败的错误布尔值

B)只记录代码将具有未定义的行为如果长度设置为负数。

您会建议哪个?还有其他任何建议吗?

我自己倾向于B。
Take a class like the following:

class Finger
{
public:

double length;
}
I''m writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn''t compile.
So... I''ve come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength. SetLength
will reject negative numbers. When supplied with a negative number it will: A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if length is
set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.




A1毫无疑问。


另请注意,A1是B的子集,因此您可以记录B但是如果您真的想要实现A1

。当您的用户抱怨异常时,您只需说''我告诉过您行为未定义''。


john



A1 without a doubt.

Also note that A1 is a subset of B, so you could document B but implement A1
if you really wanted to. When your users complain about the exception, you
just say ''I told you the behaviour was undefined''.

john


John Harrison发布:
John Harrison posted:
A1毫无疑问。

另请注意,A1是B的子集,所以你可以记录B但是如果你真的想要实现A1。当您的用户抱怨异常时,您只需说''我告诉您行为未定义''。
A1 without a doubt.

Also note that A1 is a subset of B, so you could document B but
implement A1 if you really wanted to. When your users complain about
the exception, you just say ''I told you the behaviour was undefined''.



感谢您的输入。

我自己认为这太善良了。 未定义的行为意味着

正如它所说的那样。这就像是对一个孩子说,是的,去玩游戏

的火柴和汽油 - 当你没有跑到_ME_时

你烧了自己。当然,任何有责任心的父母都会说,不要玩b $ b不要玩火柴和汽油!然后实际上是物理上

阻止孩子们进行这样的行动。


但是......我们都在这里同意成年人。

虽然我还没有做出决定。

所有输入都赞赏。

谢谢

-JKop


Thanks for the input.
I myself think that that would be too kind. "Undefined behaviour" means
exactly what it says on the tin. It''s like saying to a child, "Yes, Go play
with the matches and the petrol - Just don''t come running to _ME_ when
you''ve burned yourself". And ofcourse, any responsible parent would say,
"Don''t play with the matches and the petrol!" and then actually physically
prevent the children from performing such actions.

But... we''re all consenting adults here.
Although I still haven''t made my decision yet.
All input appreciated.
Thanks
-JKop


JKop写道:
参加如下课程:

班级手指
{
公开:

双倍长度;
}

我正在编写可重复使用的代码,作为独裁者,我要求长度
不能为负面。以下是完美的:

无符号双倍长度;

但是当然没有编译。

所以......我已经提出3种可能的解决方案:A1 A2 B。

A)创建2个独立的成员函数GetLength和SetLength。
SetLength将拒绝负数。当提供负数
时,它将:A1)抛出异常
A2)返回表示失败的错误布尔值

B)只记录代码将具有未定义的行为如果
长度设置为负数。

您会建议哪个?还有其他建议吗?

我自己倾向于B.

谢谢

-JKop
Take a class like the following:

class Finger
{
public:

double length;
}
I''m writing reusable code and, being dictator, I dictate that "length"
cannot be negative. The following would be perfect:

unsigned double length;

But ofcourse doesn''t compile.
So... I''ve come up with 3 potential solutions: A1 A2 B .
A) Create 2 separate member functions, GetLength and SetLength.
SetLength will reject negative numbers. When supplied with a negative
number it will: A1) Throw an exception
A2) Return a false boolean value indicating failure

B) Just document that the code will have undefined behaviour if
length is set to a negative number.

Which would you suggest? Any other suggestions?
I myself am leaning toward B.
Thanks
-JKop




制作一个允许范围验证的小型房产模板类,例如

这个:


#include< iostream>

#include< string>

#include< cstdlib>

使用命名空间std;


template< class T,class Modify>

class property

{

protected:

T var;

修改mod;

public:

property< T,Modify>(const T& init,const Modify& m)

:mod(m),var(init)

{}

运营商T()

{

返回var;

}


property& operator =(const T& val)

{

if(!mod(val))

throw invalid_argument(" bad property argument。 ");

var = val;

返回* this;

}

};


模板< class T>

struct RangeValidationPropModified

{

string msg;

T minval,maxval;

public:

RangeValidationPropModified< T>(字符串m,const T& minv,const T& maxv)

:msg(m),

minval(minv),

maxval(maxv)

{}


bool operator()(const T& val)

{

if(val> = minval&& val< = maxval)

{

cout<< msg;

返回true;

}

else

返回false;

}

};


int main(int argc,char * argv [])

{

property< int,RangeValidationPropModified< int> >

p(0,RangeValidationPropModified< int>(该属性正在修改!b $ b修改!\ n,0,100));


cout<< p << endl;

p = 5;

cout<< p <<结束;

p = 10;

cout<< p <<结束;

尝试

{

p = -1;

cout<< p << endl;

p = 1000;

cout<< p <<结束;

}

catch(invalid_argument ex)

{

cout<< ex.what()<<结束;

}


系统(暂停);

返回0;

}


- Pete



Make a small property template class that allows for range validation, like
this:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

template <class T, class Modify>
class property
{
protected:
T var;
Modify mod;
public:
property<T, Modify>(const T& init, const Modify& m)
: mod(m), var(init)
{}

operator T()
{
return var;
}

property& operator =(const T& val)
{
if(!mod(val))
throw invalid_argument("bad property argument.");
var = val;
return *this;
}
};

template<class T>
struct RangeValidationPropModified
{
string msg;
T minval, maxval;
public:
RangeValidationPropModified<T>(string m, const T& minv, const T& maxv)
: msg(m),
minval(minv),
maxval(maxv)
{}

bool operator() (const T& val)
{
if(val >= minval && val <= maxval)
{
cout << msg;
return true;
}
else
return false;
}
};

int main(int argc, char* argv[])
{
property<int, RangeValidationPropModified<int> >
p(0, RangeValidationPropModified<int>("the property is being
modifed!\n", 0, 100));

cout << p << endl;
p = 5;
cout << p << endl;
p = 10;
cout << p << endl;
try
{
p = -1;
cout << p << endl;
p = 1000;
cout << p << endl;
}
catch(invalid_argument ex)
{
cout << ex.what() << endl;
}

system("pause");
return 0;
}

- Pete


这篇关于拒绝负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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