| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 691 人关注过本帖
标题:复制构造函数
只看楼主 加入收藏
大剑
Rank: 2
等 级:论坛游民
帖 子:30
专家分:25
注 册:2011-11-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
复制构造函数
复制构造函数在那?
程序代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
class CStudent
{
private:
    char *p_name;
    int age;
public:
    CStudent(char *,int);
    ~CStudent() {
        if(p_name!=NULL)
            delete p_name;
        cout<<"destructor of class"<<endl;
    }
    void show(void);
};
CStudent::CStudent(char *source,int a)
{
    p_name=new(char[strlen(source)+1]);
    strcpy(p_name,source);
    age=a;
}
void CStudent::show()
{
    cout<<p_name<<" "<<age<<endl;
}
void main()
{
    CStudent *p_student=new CStudent("张三",20);
    CStudent stud1("李四",50);
    CStudent stud3(stud1);

    p_student->show();
    stud1.show();
    stud3.show();
    delete p_student;
}


出现这个状况 那位帅哥帮忙解释下。
图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 函数 
2012-02-15 13:27
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

class CStudent
{
    friend std::ostream& operator<<( std::ostream&, const CStudent& );
public:
    CStudent( const char* name, unsigned int age );
    CStudent( const CStudent& rhs );
    CStudent& operator=( const CStudent& rhs );
    ~CStudent();
    void show(void);
private:
    char* name_;
    unsigned int age_;

};

using namespace std;

CStudent::CStudent( const char* name, unsigned int age )
{
    name_ = new char[strlen(name)+1];
    strcpy( name_, name );
    age_ = age;
}
CStudent::CStudent( const CStudent& rhs )
{
    name_ = new char[strlen(rhs.name_)+1];
    strcpy( name_, rhs.name_ );
    age_ = rhs.age_;
}
CStudent& CStudent::operator=( const CStudent& rhs )
{
    if( this != &rhs )
    {
        delete[] name_;

        name_ = new char[strlen(rhs.name_)+1];
        strcpy( name_, rhs.name_ );
        age_ = rhs.age_;
    }
    return *this;
}
CStudent::~CStudent()
{
    delete[] name_;
    cout<<"destructor of class"<<endl;
}
ostream& operator<<( ostream& os, const CStudent& st )
{
    return os << st.name_ << ' ' << st.age_;
}

int main()
{
    CStudent* pst1 = new CStudent("张三",20);
    CStudent st2("李四",50);
    CStudent st3(st2);

    cout << *pst1 << endl;
    cout << st2 << endl;
    cout << st3 << endl;

    delete pst1;
    return 0;
}
2012-02-15 14:29
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:0 
所谓的复制构造其实就是拷贝构造函数,只是叫法不一样。
构造函数分为浅拷贝和深拷贝,深拷贝构造函数就是拷贝构造函数,一般来说我们不必自己写浅拷贝构造函数,因为系统会调用“=”运算符重载函数,但是如果一个类中含有指针变量的话,就要自己写拷贝构造函数了,因为涉及到类对象销毁后内存的问题!

看了你的另一篇帖子,你还是把C++的基础知识好好的掌握一下,基本太弱了!

愿用余生致力编程
2012-02-18 21:10
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 

My life is brilliant
2012-02-18 22:46
快速回复:复制构造函数
数据加载中...
 
   



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

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