| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:拷贝构造函数问题
取消只看楼主 加入收藏
巧克力饼干
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2006-3-27
收藏
 问题点数:0 回复次数:0 
拷贝构造函数问题
#include<iostream>
using namespace std;
class Student
{
public:
Student();
Student(const char*);
Student(Student&);
char* read() {return _name;}
~Student();
private:
char* _name;
};
Student::Student():_name(0)
{
cout<<"Student Defult Constructing called..."<<endl;
}
Student::Student(const char* name)
{
cout<<"Student Constructing called..."<<name<<endl;
_name = new char[strlen(name) + 1];
strcpy(_name,name);
}
Student::Student(Student& p)
{
cout<<"Student Copying Constrcting called... :"<<p._name<<endl;
_name = new char[strlen(p._name) + 1];
strcpy(_name,p._name);
}
Student::~Student()
{
cout<<"Student Destructing called...: "<<_name<<endl;
delete _name;
}
int main()
{
Student& a = Student("abc");
cout<<a.read()<<endl;
Student b = Student("xyz"); //为什么调用的不是拷贝构造函数呢?
cout<<b.read()<<endl;
Student c(Student("lmn"));//同上;
cout<<c.read()<<endl;
Student d("qwe");
Student e(d);
cout<<e.read()<<endl;
return 0;
}
搜索更多相关主题的帖子: name Student 函数 构造 char 
2006-04-05 21:56
快速回复:拷贝构造函数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011935 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved