c++指针悬挂
2012-10-09 19:56
190 查看
在C++中对于任何一个类,如果没有用户自定义的赋值运算符函数,系统会自动地为其生成默认的赋值运算函数,以完成数据成员之间的逐位复制。通常情况下,系统默认的赋值函数可以完成赋值任务,但在某些特殊情况下,如类中有指针类形式,就不能进行直接相互赋值,否则就会产生指针悬挂问题!
例如:
class Student
{
private:
char *name;
int score;
public:
Student(char *na,int s);
~Student();
void print();
};
#include"iostream"
#include"iomanip"
using namespace std;
#include"student.h"
int main()
{
Student p1("zhang",90);
Student p2("wang",80);
p2 = p1;
cout<<"p2:";
p2.print();
return 0;
}
Student::Student(char *na,int s)
{
name = new char[strlen(na) + 1];
strcpy(name,na);
score = s;
}
Student::~Student()
{
delete name;
}
void Student::print()
{
cout<<name<<setw(5)<<score<<endl;
}
程序将显示运行结果出错的信息
原因:
p2生命周期结束后,p1的指针所指的空间就不存在了,产生指针悬挂
解决的方法是:重载赋值运算符解决指针悬挂问题
class Student
{
private:
char *name;
int score;
public:
Student(char *na,int s);
Student &operator =(const Student &);
~Student();
void print();
};
#include"iomanip"
#include"iostream"
using namespace std;
#include"string.h"
#include"student.h"
int main()
{
Student p1("zhang",90);
Student p2("wang",80);
cout<<" p2: ";
p2.print();
p2 = p1;
cout<<"修改后p2: ";
p2.print();
return 0;
}
Student::Student(char *na, int s)
{
name = new char[strlen(na) + 1];
strcpy(name,na);
score = s;
}
Student &Student::operator =(const Student &p) //定义赋值运算符重载
{
if(this == &p)
return *this;
delete name; //释放掉原空间
name = new char[strlen(p.name) + 1]; //分配新空间
strcpy(name,p.name);
return *this;
}
Student::~Student()
{
delete name;
}
void Student::print()
{
cout<<name<<setw(5)<<score<<endl;
}
例如:
class Student
{
private:
char *name;
int score;
public:
Student(char *na,int s);
~Student();
void print();
};
#include"iostream"
#include"iomanip"
using namespace std;
#include"student.h"
int main()
{
Student p1("zhang",90);
Student p2("wang",80);
p2 = p1;
cout<<"p2:";
p2.print();
return 0;
}
Student::Student(char *na,int s)
{
name = new char[strlen(na) + 1];
strcpy(name,na);
score = s;
}
Student::~Student()
{
delete name;
}
void Student::print()
{
cout<<name<<setw(5)<<score<<endl;
}
程序将显示运行结果出错的信息
原因:
p2生命周期结束后,p1的指针所指的空间就不存在了,产生指针悬挂
解决的方法是:重载赋值运算符解决指针悬挂问题
class Student
{
private:
char *name;
int score;
public:
Student(char *na,int s);
Student &operator =(const Student &);
~Student();
void print();
};
#include"iomanip"
#include"iostream"
using namespace std;
#include"string.h"
#include"student.h"
int main()
{
Student p1("zhang",90);
Student p2("wang",80);
cout<<" p2: ";
p2.print();
p2 = p1;
cout<<"修改后p2: ";
p2.print();
return 0;
}
Student::Student(char *na, int s)
{
name = new char[strlen(na) + 1];
strcpy(name,na);
score = s;
}
Student &Student::operator =(const Student &p) //定义赋值运算符重载
{
if(this == &p)
return *this;
delete name; //释放掉原空间
name = new char[strlen(p.name) + 1]; //分配新空间
strcpy(name,p.name);
return *this;
}
Student::~Student()
{
delete name;
}
void Student::print()
{
cout<<name<<setw(5)<<score<<endl;
}
相关文章推荐
- C++易混点——深度解析指针悬挂和虚析构函数
- C++深层复制解决指针悬挂
- C++_指针悬挂和赋值操作符的重载
- C++指针悬挂(赋值运算符重载)
- C++悬挂指针: new与delete的一些理解
- c++ 里面由浅拷贝引起的悬挂指针
- C++ 指针悬挂和赋值操作符的重载,拷贝构造函数实现
- C++中的指针悬挂问题(内存泄漏)
- C++ 指针悬挂和赋值操作符的重载,拷贝构造函数实现
- C++_指针悬挂和赋值操作符的重载
- C++中指针悬挂问题的产生以及如何避免
- c++指针 tips
- C++之智能指针 (一 )
- C/C++语言中的void及void指针总结
- 深入探索C++对象模型--指针类型 & 多态机制
- C++指针
- C++指针和引用
- C++返回引用类型 指针的引用(转载)
- c++三种野指针
- C、C++中引用与指针的区别