尝试为 *this 指针赋值失败
Trying to assign value to *this pointer fails
我在 C++ 中得到了这个程序,如果我把最后两行放在评论中,它工作得很好:
a = b;
MyAr c(b);
我用删除它的方法对其进行了测试。问题出在我认为的 *this
指针上,当我编译并且 运行 程序闪烁片刻然后消失。你能帮我么?谢谢!
#include <iostream>
using namespace std;
class MyAr {
int *p;
int len;
public:
MyAr();
MyAr(int a);
MyAr(const MyAr& ob);
~MyAr();
MyAr& operator=(const MyAr& ox) { *this = ox; }
int& operator[](int i) { return p[i]; }
int length();
};
MyAr::MyAr() : p(0), len(0) {}
MyAr::MyAr(int a) : p(new int[a]), len(a) {}
MyAr::MyAr(const MyAr& ob) { *this = ob; }
MyAr::~MyAr() { delete p; }
int MyAr:: length(){
return len;
}
int main(){
MyAr a;
MyAr b(10);
for(int i=0; i< b.length(); ++i)
b[i] = i;
a = b;
MyAr c(b);
system("pause");
return(0);
}
定义
MyAr& MyAr::operator=(const MyAr& ox) { *this = ox; }
是递归的,因为赋值 *this = ox
再次调用重载赋值运算符。所以你有无限递归(大概导致你的程序最终终止)。
这与调用以下函数相同:
void f() { f(); }
或者,在英语中,您已经将 "assign from value ox
" 的含义定义为 "assign from value ox
",而您真正需要做的是根据构成结构来定义它的含义属于你的类型!
例如:
MyAr& MyAr::operator=(const MyAr& ox) {
delete [] a;
a = nullptr;
len = 0;
return *this;
}
(这可能没有您想要的语义;请根据口味进行修改。)
您将在 C++ 中做的最重要的事情之一就是学习编写正确的构造函数和析构函数:
#include <cassert>
#include <cstring>
#include <utility>
class MyAr {
int *p;
int len;
public:
MyAr() : p(nullptr), len(0) {};
MyAr(int a) : p(new int[a]), len(a) {};
// because we are overloading the destructor, rule of 3 is required (c++03)
MyAr(const MyAr& ob)
: p( nullptr), len(ob.len)
{
if (len) {
assert(ob.p);
p = new int[len];
std::memcpy(p, ob.p, len);
}
}
MyAr& operator=(const MyAr& r)
{
MyAr tmp(r);
swap(tmp);
return *this;
}
~MyAr() {
// note: delete []
delete [] p;
}
// or rule of 5 (c++11)
#if __cplusplus >= 201103L
MyAr(MyAr&& r)
: p(r.p)
, len(r.len)
{
r.p = nullptr;
r.len = 0;
}
MyAr& operator=(MyAr&& r)
{
auto tmp = MyAr(std::move(r));
swap(tmp);
return *this;
}
#endif
void swap(MyAr& other)
{
using std::swap;
swap(p, other.p);
swap(len, other.len);
}
int& operator[](int i) { return p[i]; }
int length();
};
我在 C++ 中得到了这个程序,如果我把最后两行放在评论中,它工作得很好:
a = b;
MyAr c(b);
我用删除它的方法对其进行了测试。问题出在我认为的 *this
指针上,当我编译并且 运行 程序闪烁片刻然后消失。你能帮我么?谢谢!
#include <iostream>
using namespace std;
class MyAr {
int *p;
int len;
public:
MyAr();
MyAr(int a);
MyAr(const MyAr& ob);
~MyAr();
MyAr& operator=(const MyAr& ox) { *this = ox; }
int& operator[](int i) { return p[i]; }
int length();
};
MyAr::MyAr() : p(0), len(0) {}
MyAr::MyAr(int a) : p(new int[a]), len(a) {}
MyAr::MyAr(const MyAr& ob) { *this = ob; }
MyAr::~MyAr() { delete p; }
int MyAr:: length(){
return len;
}
int main(){
MyAr a;
MyAr b(10);
for(int i=0; i< b.length(); ++i)
b[i] = i;
a = b;
MyAr c(b);
system("pause");
return(0);
}
定义
MyAr& MyAr::operator=(const MyAr& ox) { *this = ox; }
是递归的,因为赋值 *this = ox
再次调用重载赋值运算符。所以你有无限递归(大概导致你的程序最终终止)。
这与调用以下函数相同:
void f() { f(); }
或者,在英语中,您已经将 "assign from value ox
" 的含义定义为 "assign from value ox
",而您真正需要做的是根据构成结构来定义它的含义属于你的类型!
例如:
MyAr& MyAr::operator=(const MyAr& ox) {
delete [] a;
a = nullptr;
len = 0;
return *this;
}
(这可能没有您想要的语义;请根据口味进行修改。)
您将在 C++ 中做的最重要的事情之一就是学习编写正确的构造函数和析构函数:
#include <cassert>
#include <cstring>
#include <utility>
class MyAr {
int *p;
int len;
public:
MyAr() : p(nullptr), len(0) {};
MyAr(int a) : p(new int[a]), len(a) {};
// because we are overloading the destructor, rule of 3 is required (c++03)
MyAr(const MyAr& ob)
: p( nullptr), len(ob.len)
{
if (len) {
assert(ob.p);
p = new int[len];
std::memcpy(p, ob.p, len);
}
}
MyAr& operator=(const MyAr& r)
{
MyAr tmp(r);
swap(tmp);
return *this;
}
~MyAr() {
// note: delete []
delete [] p;
}
// or rule of 5 (c++11)
#if __cplusplus >= 201103L
MyAr(MyAr&& r)
: p(r.p)
, len(r.len)
{
r.p = nullptr;
r.len = 0;
}
MyAr& operator=(MyAr&& r)
{
auto tmp = MyAr(std::move(r));
swap(tmp);
return *this;
}
#endif
void swap(MyAr& other)
{
using std::swap;
swap(p, other.p);
swap(len, other.len);
}
int& operator[](int i) { return p[i]; }
int length();
};