4

我创建了一个类,该类对可变粒度每日计划中的时间段进行建模,例如,第一个时间段是 30 分钟,但第二个时间段可以是 40 分钟,第一个可用时间段开始于(一个可比较的值到) 1。

我现在要做的是以某种方式定义该类采用的最大和最小允许值,为此我有两个实际问题:

1.-以这种方式为自定义类定义绝对最小值和最大值是否有意义?或者更好的是,在给定类定义的关系运算符的情况下,一个值总是比较低于该类型的任何其他可能值就足够了,被定义为最小值?(同样适用于最大值)

2.-假设上一个问题有一个以“是”(或“是但......”)为模型的答案,我如何定义这样的最大值/最小值?我知道有,std::numeric_limits<>但从我读到的内容来看,它是用于“数字类型”的。我是否将其解释为“表示为数字”的意思,还是可以做出更广泛的假设,例如“用数字表示”或“与整数有对应关系”?毕竟,为日期类和字典类定义最小值和最大值是有意义的,但numeric_limits可能不适用于这些用途(我没有太多经验)。另外,numeric_limits还有很多额外的成员和信息,我不知道该怎么做。如果我使用numeric_limits,C++ 提供了哪些其他众所周知/广泛使用的机制来指示类的可用值范围?

4

3 回答 3

2

Having trouble making sense of your question. I think what you're asking is whether it makes sense to be assertive about the class's domain (that data which can be fed to it and make sense), and if so how to be assertive.

The first has a very clear answer: yes, absolutely. You want your class to be, "...easy to use correctly and difficult to use incorrectly." This includes making sure the clients of the class are being told when they do something wrong.

The second has a less clear answer. Much of the time you'll simply want to use the assert() function to assert a function or class's domain. Other times you'll want to throw an exception. Sometimes you want to do both. When performance can be an issue sometimes you want to provide an interface that does neither. Usually you want to provide an interface that can at least be checked against so that the clients can tell what is valid/invalid input before attempting to feed it to your class or function.

The reason you might want to both assert and throw is because throwing an exception destroys stack information and can make debugging difficult, but assert only happens during build and doesn't actually do anything to protect you from running calculations or doing things that can cause crashes or invalidate data. Thus asserting and then throwing is often the best answer so that you can debug when you run into it while testing but still protect the user when those bugs make it to the shelf.

For your class you might consider a couple ways to provide min/max. One is to provide min/max functions in the class's interface. Another might be to use external functionality and yes, numeric_limits might just be the thing since a range is sometimes a type of numeric quantity. You could even provide a more generic interface that has a validate_input() function in your class so that you can do any comparison that might be appropriate.

The second part of your question has a lot of valid answers depending on a lot of variables including personal taste.

于 2010-05-20T17:36:53.187 回答
2

作为您的计划/时段代码的设计者,您需要多少灵活性/实用性取决于您。

两种简单的方法是在该类中定义您自己的值

const long MIN_SLOT = 1;
const long MAX_SLOT = 999; // for example

或者定义另一个包含定义的类

class SchedLimits{

public:
const static long MIN_SLOT = 1;
const static long MAX_SLOT = 999;
}

最简单的是枚举。(感谢让我想起这些的评论者)

enum {MIN_SLOT = 1, MAX_SLOT = 999};
于 2010-05-20T17:41:30.773 回答
1

Just create some const static members that reflect the minimums and maximums.

于 2010-05-20T17:35:54.953 回答