| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 884 人关注过本帖
标题:常数据成员的深拷贝,const+字符型指针 ,如何写深拷贝的代码?
只看楼主 加入收藏
ZER0O0
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-7-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
常数据成员的深拷贝,const+字符型指针 ,如何写深拷贝的代码?
程序代码:
/**
没有const。
char *name;
**/
#include <iostream>
#include <cstring>
using namespace std;

class Student
{
public:
    Student(int n,char *na,float s):no(n),score(s)
    {
        name=new char[strlen(na)+1];
        strcpy(name,na);
    }
    Student(const Student& s):no(s.no),score(s.score)
    {
        name = new char[strlen(s.name)+1];
        strcpy(name,s.name);
    }
    ~Student()
    {
        if(name!=NULL)
            delete []name;
    }
    void display()const
    {
        cout << no << " " << name << " " << score <<endl;
    }
protected:

private:
    const int no;
    char *name;
    const float score;
};

int main()
{
    const Student stu1(1,"wangming",99);
    stu1.display();
    const Student stu2(stu1);
    stu2.display();
    return 0;
}

程序代码:
/**
包含const。
const char * name;
**/
#include <iostream>
#include <cstring>
using namespace std;

class Student
{
public:
    Student(int n,char *na,float s):no(n),score(s)
    {
        name=new char[strlen(na)+1];
        strcpy(name,na);
    }
    Student(const Student& s):no(s.no),score(s.score)
    {
        name = new char[strlen(s.name)+1];
        strcpy(name,s.name);
    }
    ~Student()
    {
        if(name!=NULL)
            delete []name;
    }
    void display()const
    {
        cout << no << " " << name << " " << score <<endl;
    }
protected:

private:
    const int no;
    const char *name;
    const float score;
};

int main()
{
    const Student stu1(1,"wangming",99);
    stu1.display();
    const Student stu2(stu1);
    stu2.display();
    return 0;
}
搜索更多相关主题的帖子: const char name Student score 
2018-07-14 13:57
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:10 
程序代码:
class Foo {
    private:
        const char *name;
    public:
        Foo(const char *name) : name(name) {}
        Foo(const Foo &other) : name(other.name) {}
        void show() const {
            cout << this->name << endl;
        }
};
2018-07-14 20:13
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
如果一定要给字符串分配内存, 那就这样
程序代码:
class Foo {
    private:
        const char **name;
    public:
        Foo(const char *name) : name {new const char * {name}} {}
        Foo(const Foo &other) : name {new const char * {*other.name}} {}
        ~Foo() {
            delete this->name;
        }
        void show() const {
            cout << *this->name << endl;
        }
};
2018-07-14 20:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
设计错误
如果你想定义一个不可修改的对象,那应该是定义 const Student 就行了,而不是定义一个内部全是const成员的Student.
当然,你一定要这么做也可以,和其它成员同样方式处理,即放到初始化列表中
程序代码:
#include <iostream>
#include <cstring>

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';
}

2018-07-16 09:04
快速回复:常数据成员的深拷贝,const+字符型指针 ,如何写深拷贝的代码?
数据加载中...
 
   



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

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