- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
回答问题前请引用以下程序。在评论中解释了代码。
所以我的问题是赋值运算符重载如何处理 new() 分配内存失败的情况。
例如 Obj1
持有字符串 "GeeksQuiz"
。将 Obj2
分配给 Obj1
。在赋值过程中(在赋值运算符重载函数中),我们首先释放 Obj1
,然后用 Obj2
值重新创建 Obj1
。那么在 new
无法分配内存的情况下如何保留旧的 Obj1
值?因为我们在函数启动时释放了 Obj1
值。
我想要的只是在分配操作失败时为 Obj1
保留旧值。
请帮帮我。我想要完美的代码,没有任何内存泄漏覆盖所有场景。提前致谢
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char *string_data;
int size;
public:
String(const char *str = NULL); // constructor
~String() { delete [] string_data; }// destructor
void print() { cout << string_data << endl; } // Function to print string
String& operator = (const String &); // assignment operator overload
};
String::String(const char *str) // Constructor
{
size = strlen(str);
string_data = new char[size+1];
if (string_data != NULL)
strcpy(string_data, str);
else
cout<<"compiler failed to allocate new memory";
}
String& String::operator = (const String &str) // assignment operator overload
{
if(this != &str)
{
delete [] string_data; // Deleting old data and assigning new data below
size = str.size;
string_data = new char[size+1];
if(string_data != NULL) // This condition is for cheking new memory is success
strcpy(string_data, str.string_data);
else
cout<<"compiler failed to allocate new memory"; // My quetsion comes in this scenario...
}
return *this;
}
int main()
{
String Obj1("GeeksQuiz");
String Obj2("stackoverflow");
Obj1.print(); // Printing Before assigment
Obj2.print();
Obj1 = Obj2; // Assignment is done.
Obj1.print(); // Printing After assigment
Obj2.print();
return 0;
}
最佳答案
首先,实现一个健壮的字符串是很困难的,除非你想以学习为目的总是使用 std::string
。
然后考虑operator new始终返回非空指针(除非您还实现了非标准的自定义 new
运算符),如果分配数据失败,则会抛出 std::bad_alloc
异常.如果你想处理分配失败的情况,你需要添加一个 try-catch block
char *data = NULL;
try {
data = new char[str.size + 1];
} catch (std::bad_alloc &e) {
std::cout << "Allocation failed: " << e.what() << std::endl;
throw; // <- You probably want to rethrow the exception.
}
strcpy(data, str.string_data);
delete [] string_data;
string_data = data;
size = str.size;
重要的部分是在抛出异常时让您的类保持一致状态,这就是为什么您必须首先分配新数据,然后如果成功,则删除旧数据。然而,bad_alloc 异常很少在类级别处理,通常您让异常被抛出(这就是我在代码示例中重新抛出的原因)并让客户端代码处理它。
如果你真的希望你的代码是异常证明我会建议使用智能指针,并且如前所述,在这种情况下使用 std::string
。
关于c++ - 赋值运算符重载 : Error Handling scenario,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32734673/
我有以下代码: String text = "[\"Scenario 1\", \"Scenario 2\"]" 我需要将 "text" 转换为 Array。我知道我可以替换 [ ] "并执行 spl
我知道 here 中的场景和场景大纲之间的区别。 Scenario states 以更抽象的方式测试的一般点。同时,scenario outline通过几个示例来帮助执行场景。 所以,我们通常会写fe
我在 Cucumber 中有我的特征文件,我正在使用 Given, When, Then 语法 (Gherkin) 在其中编写我的场景,并在步骤定义文件中有相应的步骤。我有一个场景,我正在检查特定数据
我正在尝试运行一些测试作为学习 cucumber 的一部分。但我得到的结果是 0 个场景。在这里,我添加了我编写的代码: 登录功能和- Feature: Application Login S S
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个像这样的 XML
我一直在尝试使用 Calabash 来测试一些简单的 iOS 应用程序。我能够在模拟器上运行该应用程序,并且所有步骤都运行良好。现在我想在 iOS 设备上测试它,但我遇到了一些问题。我用所有必要的信息
我有以下 MySQL 实例以及复制设置: S1 -----> (M1 M2),其中: M1 - M2 是一个多主复制设置, S1 - 复制在主 M1 上完成的写入的从站。 现在,我正在尝试使用 ch
我正在使用 Specflow 编写一套模拟月薪的场景,验证每个月的计算付款以及最后的年终数据。 每个月的结果都是累加的,所以后面的每一个场景都依赖于上个月的加减。支付计算通过第三方工具写入数据库,因此
有两个需要的功能:注册时设置密码和更改密码,如果用户忘记了。用户注册时,密码长度必须至少为4个字符;当更改通过时 - 至少 5 个字符。 View 对于注册和更改通行证很常见。显然,还存在 2 个操作
正如主题所述,我很难想象在现实生活中的示例中何时何地使用可恢复异常,以及我们可以通过使用它们获得哪些有效优势。 我能想象的是,一个子系统已连接,比方说通过 RFC 连接到一个 session ,该 s
我正在设计一个游戏,其中一个角色有很多元素,而这些元素可以有多种类型。有一个角色表,以及十二个按类型(例如武器、盔甲和各种其他项目类型)分割的可能项目的不同表。 我想制作一个表来保存这些项目类型的实例
我正在开发一个 Windows 8 Metro 风格的应用程序,在大学的学术项目中使用开发者预览版。我们必须使用 MVVM 模式。 在主页中,我们有一个带有按钮的 Metro 风格主菜单。根据 MVV
目前,我正在使用BDD进行项目,其中涉及一些导入语句,例如 导入 cucumber .api。场景; 但是我找不到合适的依赖项 目前我有依赖 compile 'commons-lang:commons
考虑以下因素: @Given("^this stuff:$") public void this_stuff(List stuffList) throws Throwable { stuffL
我正在寻找如何实现这个场景:我有函数内部的逻辑代码,现在我希望能够在单独的线程中执行此功能。现在我拥有的是这个 .. 的原始实现我简单地初始化线程,在它的启动/运行方法中我保留了函数逻辑。我怎样才能让
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我有一个相当流行的场景,其中有一个包含进度条和 ListView 的 fragment 。我想在使用 Retrofit 获取数据后填充 ListView。现在,一旦响应返回,就需要使进度条不可见。 存
回答问题前请引用以下程序。在评论中解释了代码。 所以我的问题是赋值运算符重载如何处理 new() 分配内存失败的情况。 例如 Obj1 持有字符串 "GeeksQuiz"。将 Obj2 分配给 Obj
在下面的代码中,我只是想看看我是否能够从每个线程将一个元素插入到数组中。它按预期工作。但后来我想知道,在什么情况下这里会出现竞争条件。我真的需要 volatile 还是信号量?我尝试删除信号量和 vo
我是一名优秀的程序员,十分优秀!