断言是不太安全的...我说的是你传地址就不要想删除了...因为字符串被当成常量用的...所以指针是没办法删除的....就是说你调用delete[]时候引发错误
下面是对的,我上面没删除指针...
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
public:
Student(char *name = "qwe", int stature = 180, int avoirdupois = 75);
Student(const Student &stu);
void demo();
public:
~Student();
private:
char * m_name;
int m_stature, m_avoirdupois;
};
Student::Student(char *name, int stature, int avoirdupois)
{
m_name = new char[sizeof(name)+1];
strcpy(m_name,name);
this->m_stature = stature;
this->m_avoirdupois = avoirdupois;
cout << "缺省构造函数" << endl;
cout << "姓名:" << this->m_name <<",身高:" << this->m_stature << ",体重:" << this->m_avoirdupois << endl;
}
void Student::demo()
{
cout << "姓名:" << this->m_name <<",身高:" << this->m_stature << ",体重:" << this->m_avoirdupois << endl;
}
Student::~Student()
{
delete[] m_name;
}
int main()
{
Student s("qwt1",185,132);
s.demo();
return 0;
}
下面是对的,我上面没删除指针...
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
public:
Student(char *name = "qwe", int stature = 180, int avoirdupois = 75);
Student(const Student &stu);
void demo();
public:
~Student();
private:
char * m_name;
int m_stature, m_avoirdupois;
};
Student::Student(char *name, int stature, int avoirdupois)
{
m_name = new char[sizeof(name)+1];
strcpy(m_name,name);
this->m_stature = stature;
this->m_avoirdupois = avoirdupois;
cout << "缺省构造函数" << endl;
cout << "姓名:" << this->m_name <<",身高:" << this->m_stature << ",体重:" << this->m_avoirdupois << endl;
}
void Student::demo()
{
cout << "姓名:" << this->m_name <<",身高:" << this->m_stature << ",体重:" << this->m_avoirdupois << endl;
}
Student::~Student()
{
delete[] m_name;
}
int main()
{
Student s("qwt1",185,132);
s.demo();
return 0;
}
学习需要安静。。海盗要重新来过。。