定义新类型 [英] defining new types
问题描述
我确定这个答案相当简单......但我如何定义一个带范围检查的新
类型?
示例:我想定义一个类似于double的新类型,除了你
只能给它0.0到100.0之间的值。我也希望它尽可能地像一个
加倍,除了当它设置为无效数字的
时抛出异常。
想法?
谢谢,
Joe
I''m sure there''s a fairly easy answer for this... but how can I define a new
type with range checking?
Example: I want to define a new type that''s like a double, except that you
can only give it values from 0.0 to 100.0. I''d also like it to act like a
double as much as possible, except that an exception is thrown when it''s set
to an invalid number.
Ideas?
Thanks,
Joe
推荐答案
* Joe Laughlin:
* Joe Laughlin:
我确定这个答案相当简单......但我怎么能定义一个新的
示例:我想定义一个类似于double的新类型,除了你只能给它0.0到100.0之间的值。我也希望它尽可能地像双击一样,除了当它被设置为无效数字时抛出异常。
想法?
I''m sure there''s a fairly easy answer for this... but how can I define a new
type with range checking?
Example: I want to define a new type that''s like a double, except that you
can only give it values from 0.0 to 100.0. I''d also like it to act like a
double as much as possible, except that an exception is thrown when it''s set
to an invalid number.
Ideas?
原始C ++和C之间的区别在于C ++有类。
类是一种类型。
要定义新类型,请定义一个类。
提供您希望类型具有的操作。
>
给自己一本好的C ++书,比如说加速的C ++。
-
答:因为它弄乱了人们通常阅读文本的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和e-最令人烦恼的是什么?邮件?
The difference between original C++ and C was that C++ had classes.
A class is a type.
To define a new type, define a class.
Supply the operations you want the type to have.
Get yourself a good C++ book, e.g. "Accelerated C++".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Joe Laughlin写道:
Joe Laughlin wrote:
我确定这个答案相当简单......但我怎么能用范围检查定义一个新的
类型?
示例:我想定义一个类似于double的新类型,除了你只能从它给它的值0.0到100.0。我也希望它尽可能地像双击一样,除了当它被设置为无效数字时抛出异常。
想法?
I''m sure there''s a fairly easy answer for this... but how can I define a new
type with range checking?
Example: I want to define a new type that''s like a double, except that you
can only give it values from 0.0 to 100.0. I''d also like it to act like a
double as much as possible, except that an exception is thrown when it''s set
to an invalid number.
Ideas?
这个google群组链接指向最近关于comp.std.c ++的讨论。
http://tinyurl.com/6zyg9
Joe Laughlin发布:
Joe Laughlin posted:
我确定这个答案相当简单......但是如何定义一个带范围检查的新类型?
示例:我想定义一个类似于double的新类型,除了
你只能给它0.0到100.0之间的值。我也希望它尽可能地像双倍一样行动,除非当它被设置为无效数字时会抛出异常
想法?
谢谢,
Joe
I''m sure there''s a fairly easy answer for this... but how can I define
a new type with range checking?
Example: I want to define a new type that''s like a double, except that
you can only give it values from 0.0 to 100.0. I''d also like it to act
like a double as much as possible, except that an exception is thrown
when it''s set to an invalid number.
Ideas?
Thanks,
Joe
用你的大脑:
class RestrictiveDouble >
{
public:class bad_proposal {};
private:
双数据;
Set(建议使用双const)
{
if(建议> 100 ||建议< 0)抛出bad_proposal ;
数据=建议;
}
公开:
RestrictiveDouble& operator =(建议双const)
{
套装(建议);
}
//复制构造函数是不必要的
//在这里放置一个构造函数
//在这里放一个运算符
};
我会为你完成它,但是一半之后我认为它可能会为你做好功课。
-JKop
Use your brain:
class RestrictiveDouble
{
public: class bad_proposal {};
private:
double data;
Set(double const proposed)
{
if (propose > 100 || proposed < 0) throw bad_proposal;
data = proposed;
}
public:
RestrictiveDouble& operator=(double const proposed)
{
Set(proposed);
}
//Copy constructor is unnecessary
//Put a constructor here
//Put an operator double here
};
I would''ve finished it for you, but then half way through I thought it may
have been homework for you.
-JKop
这篇关于定义新类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!