拷贝构造函数问题,下面这段代码 ,尤其是f1(),什么问题会发生咧? Compile error? Run-time problem?

[复制链接]
查看11 | 回复4 | 2010-9-10 19:45:38 | 显示全部楼层 |阅读模式
class Student
{
public:

char *name;

Student(char *name);
};

Student::Student(char *_name)
{

name = (char *) malloc(strlen(_name) + 1);

strcpy(name, _name);
}
void f1(void)
{

Student foo("Foo");
}

回复

使用道具 举报

千问 | 2010-9-10 19:45:38 | 显示全部楼层
应该定义复制构造函数Student::Student(const Student &stu){name = new char[strlen(stu.name)+1];strcpy(name, stu.name);}这个Student::Student(const char *_name) 最好。。。。
回复

使用道具 举报

千问 | 2010-9-10 19:45:38 | 显示全部楼层
你好!!!在线交流!!不是很理解的问题??
回复

使用道具 举报

千问 | 2010-9-10 19:45:38 | 显示全部楼层
乖乖,这问题好复杂,都看不懂!
回复

使用道具 举报

千问 | 2010-9-10 19:45:38 | 显示全部楼层
主函数main呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行