gpt4 book ai didi

c++ - 抛出异常与返回结果代码

转载 作者:行者123 更新时间:2023-11-30 04:25:57 30 4
gpt4 key购买 nike

我正在创建一个库。我想制作一个固定长度的字符串类。

#include <string>
#include <iostream>

#define OK 0
#define TOO_LONG 1
#define UNALLOWED_CHARACTERS 2

struct MyString {
MyString(int l)
: m_length(l) { }
struct exception {
exception(int t, MyString *p)
: type(t), ptr(p) { }
int type;
MyString *ptr;
};
int set(const std::string& name);
void set2(const std::string& name) throw(exception);

std::string m_str;
int m_length;
};

int MyString::set(const std::string& s)
{
if(s.size() > 64) {
return TOO_LONG;
} else if(s.find('~') != std::string::npos) {
return UNALLOWED_CHARACTERS;
} else {
m_str = s;
return OK;
}
}

void MyString::set2(const std::string& s) throw(exception)
{
if(s.size() > m_length) {
throw exception(TOO_LONG, this);
} else if(s.find('~') != std::string::npos) {
throw exception(UNALLOWED_CHARACTERS, this);
} else {
m_str = s;
}
}

int main()
{
using namespace std;
//OPTION 1
{
MyString s1(10);
MyString s2(10);
int code;

code = s1.set("abcdefghijX");
switch(code) {
case TOO_LONG:
//handle <--
break;
case UNALLOWED_CHARACTERS:
//handle
break;
default:
//ok!
break;
}

code = s2.set("abcdefghi~");
switch(code) {
case TOO_LONG:
//handle
break;
case UNALLOWED_CHARACTERS:
//handle <--
break;
default:
//ok!
break;
}
}

//OPTION 2
{
MyString s1(10);
MyString s2(10);
try {
s1.set2("abcdefghijX");
s2.set2("abcdefghi~");

} catch(MyString::exception &e) {
cerr << "MyString::exception: ";
auto p = e.ptr;
if(p == &s1) cerr << "s1 ";
else if(p == &s2) cerr << "s2 ";
switch(e.type) {
case TOO_LONG: cerr << "too long"; break;
case UNALLOWED_CHARACTERS: cerr << "unallowed characters"; break;
}
cerr << endl;
}

}
}

我不知道应该使用哪个版本的 MyString::set()。这种情况下的惯例是什么?出于演示目的,我在此示例中使用了 STL。

最佳答案

除非有特殊原因,否则模仿标准库函数的行为是个好主意。顺便说一句,从 tr1 开始,STL 内置了一个固定长度的字符串类。让我们看看它做了什么。我手边唯一的示例实现是 Visual C++ 2010。


std::tr1::array<int,5> arry;
arry[10] = 42; // Oopsie. There is no element 10.

当以“调试”版本编译和运行时,我得到一个assert 失败。当为“发布”编译时,冒犯性声明悄悄地做了……什么都没有。它被优化得不存在了。好吧,也许这并不总是人们想要的。忘记我所说的模仿 STL,或者至少是 Microsoft 的实现。意识列车继续……

我认为可以公平地说,如果程序试图设置一个超出范围的单元格,那是程序中的一个逻辑错误。在任务关键型软件中,使用适当的代码来处理此类情况并从中恢复可能是个好主意,同时努力确保它永远不会、永远不会发生。

所以答案是,抛出一个 std::out_of_range 类型的异常。

就这样吧。

关于c++ - 抛出异常与返回结果代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817612/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com