VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 668|回复: 2

[已解决]36课作业请求帮助

[复制链接]
00_avatar_middle
最佳答案
0 
在线会员 发表于 2021-9-4 22:07:03 | 显示全部楼层 |阅读模式
#include <iostream>
#include <string>
using namespace std;


class CStudent
{
public:
        char *p_name;
        char sex;
        int num;
        int age;
        CStudent(){};
        CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age)
        {
                p_name = NULL;
                int n_len = 0;
                if (pname)
                {
                        n_len = strlen(pname);
                }

                if (n_len > 0)
                {
                        p_name = new char[n_len + 1];
                        memset(p_name, 0, n_len + 1);
                        strcpy(p_name, pname);
                }
        }
        CStudent(const CStudent& stud)
        {
                int n_len = 0;
                n_len = strlen(stud.p_name);
                p_name = new char[n_len + 1];
                memset(p_name, 0, n_len + 1);
                strcpy(p_name, stud.p_name);
                num = stud.num;
                age = stud.age;
        }
        ~CStudent();
};
CStudent::~CStudent()
{
        if (p_name)
        {
                delete[] p_name;
                p_name = NULL;
        }
}

void test()
{
        CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
        CStudent stud_2(stud_1);
}

int main(int argc, char* argv[])
{
        test();
        return 0;
}
想问一下大佬  
CStudent(const CStudent& stud)
        {
                int n_len = 0;
                n_len = strlen(stud.p_name);
                p_name = new char[n_len + 1];
                memset(p_name, 0, n_len + 1);
                strcpy(p_name, stud.p_name);
                num = stud.num;
                age = stud.age;
        }
这句话的作用是啥 我看不懂啊
越学越不会了36课作业请求帮助
最佳答案
00_avatar_small
2021-9-7 09:19:42
楼上说的对,补充一下
void test()
{
        CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
       CStudent stud_2(stud_1);  //就是为了这句话
}
那个构造函数的参数是一个静态的类对象本身的引用
作用是根据一个已经存在的对象 复制创建一个新对象

其实这个构造函数C++自动提供了一个默认的复制构造函数,如果你不写他就默认赋值
但是你现在这个demo他有点区别,成员函数中有一个字符串指针,这就涉及到字符数组多大,是否会造成内存溢出的问题 所以在自己写的这个构造函数中就有
CStudent(const CStudent& stud)
        {
                int n_len = 0;
                n_len = strlen(stud.p_name);
                p_name = new char[n_len + 1];
               memset(p_name, 0, n_len + 1);
                strcpy(p_name, stud.p_name);
                num = stud.num;
                age = stud.age;
        }
对字符串进行了长度的约束,再拷贝复制
下面那俩就不解释了




上一篇:结构体数组的指针
下一篇:球球了,兄弟们,帮我康康吧
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-9-6 18:02:00 | 显示全部楼层
叫做拷贝构造器
00_avatar_middle
最佳答案
10 
在线会员 发表于 2021-9-7 09:19:42 | 显示全部楼层    本楼为最佳答案   
bestAnswer
楼上说的对,补充一下
void test()
{
        CStudent stud_1 = { "zhangsan",'m', 1001, 20 };
       CStudent stud_2(stud_1);  //就是为了这句话
}
那个构造函数的参数是一个静态的类对象本身的引用
作用是根据一个已经存在的对象 复制创建一个新对象

其实这个构造函数C++自动提供了一个默认的复制构造函数,如果你不写他就默认赋值
但是你现在这个demo他有点区别,成员函数中有一个字符串指针,这就涉及到字符数组多大,是否会造成内存溢出的问题 所以在自己写的这个构造函数中就有
CStudent(const CStudent& stud)
        {
                int n_len = 0;
                n_len = strlen(stud.p_name);
                p_name = new char[n_len + 1];
               memset(p_name, 0, n_len + 1);
                strcpy(p_name, stud.p_name);
                num = stud.num;
                age = stud.age;
        }
对字符串进行了长度的约束,再拷贝复制
下面那俩就不解释了
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-8 18:15

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表