找错,谢谢
#include<iostream>#include<string>
using namespace std ;
class Person
{
public :
Person ( const Person &p ) ;
~Person () ;
void setAge ( int x ) { age = x ; }
void print () ;
private :
char * name ;
int age ;
} ;
Person :: Person ( const Person &p )
{
name = new char [ strlen ( p.name ) + 1 ] ;
strcpy ( name , p.name ) ;
age = p.age ;
cout << " Copy constructor called ! " << endl ;
}
Person :: ~Person ()
{
cout << " Destructor called ! " << endl ;
delete name ;
}
void Person :: print () { cout << " name : " << name << " age : "<< age << endl ; }
int main ()
{
Person p1 ( " 张三 " , 21 ) ;
Person p2 = p1 ;
p1.setAge (1) ;
p2.setAge (2) ;
p1.print () ;
p2.print () ;
return 0 ;
}
错在了哪里








程序代码:
谢谢你的多次回答 ,但是用你改正后的代码执行时难道没有出现错误提示框吗? 程序只调用了一次构造函数,但调用了两次析构函数
不好意思,我搞错了。厉害厉害