从你的上个帖子中的代码来看,你的老师教的是假C++(会点C,补点儿C++语法,就误以为自己会C++了)
所以我写个真正的C++代码,估计反而不能符合你老师的要求。
略尽人事吧
程序代码:
#include <iostream>
#include <cstring>
class homepage
{
public:
homepage( const char* photo, int height, int width ) try : photo_(new char[strlen(photo)+1]), height_(height), width_(width)
{
strcpy( photo_, photo );
}
catch( ... )
{
throw;
}
homepage( const homepage& obj ) try : photo_(new char[strlen(obj.photo_)+1]), height_(obj.height_), width_(obj.width_)
{
strcpy( photo_, obj.photo_ );
}
catch( ... )
{
throw;
}
homepage( homepage&& obj ) noexcept : photo_(obj.photo_), height_(obj.height_), width_(obj.width_)
{
obj.photo_ = nullptr;
}
homepage& operator=( const homepage& obj )
{
if( this != &obj )
{
delete[] photo_;
try {
photo_ = new char[strlen(obj.photo_)+1];
}
catch( ... ) {
throw;
}
strcpy( photo_, obj.photo_ );
height_ = obj.height_;
width_ = obj.width_;
}
return *this;
}
homepage& operator=( homepage&& obj ) noexcept
{
if( this != &obj )
{
delete[] photo_;
photo_ = obj.photo_;
height_ = obj.height_;
width_ = obj.width_;
obj.photo_ = nullptr;
}
return *this;
}
~homepage()
{
delete[] photo_;
}
private:
char* photo_;
int height_;
int width_;
friend std::ostream& operator<<( std::ostream& os, const homepage& obj )
{
return os << "photo = " << obj.photo_ << ", height = " << obj.height_ << ", width = " << obj.width_;
}
};
using namespace std;
int main( void )
{
homepage a = { "a", 1, 2 };
cout << a << endl;
homepage a2 = a;
cout << a2 << endl;
homepage a3 = std::move(a2);
cout << a3 << endl;
a = a3;
cout << a << endl;
a = std::move(a3);
cout << a << endl;
return 0;
}