常数据成员的深拷贝,const+字符型指针 ,如何写深拷贝的代码?

#include

#include


class Student

{

public:

    Student( int no, const char* name, float score ) : no_(no), name_(strcpy(new char[strlen(name)+1],name)), score_(score)

    {

    }

    Student( const Student& s ) : no_(s.no_), name_(strcpy(new char[strlen(s.name_)+1],s.name_)), score_(s.score_)

    {

    }

    ~Student()

    {

        delete[] name_;

    }


protected:

    Student& operator=( const Student& s );


private:

    const int no_;

    const char* name_;

    const float score_;


    friend std::ostream& operator<<( std::ostream& os, const Student& s )

    {

        return os << s.no_ << ' ' << s.name_ << ' ' << s.score_;

    }

};


using namespace std;


int main( void )

{

    Student stu1( 1, "wangming", 99 );

    Student stu2 = stu1;

    cout << stu1 << '\n';

    cout << stu2 << '\n';

}


请使用浏览器的分享功能分享到微信等