VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 825|回复: 3

[已解决]学习C++0基础入门第36课,请教拷贝构造函数问题

[复制链接]
88_avatar_middle
最佳答案
0 
donate_vip 发表于 2021-3-12 08:44:35 | 显示全部楼层 |阅读模式
本帖最后由 sunguofu8 于 2021-3-12 09:38 编辑

老师们好!我在学习C++0基础入门的36课,做完成作业时遇到如下问题,麻烦您给指点一下,多谢您!按着课上讲的,由于多delete一次name的内存空间,所以会报错中断的,我采用拷贝构造函数时也一样。代码如下:
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
//#include "Student.h"
using namespace std;

class CStest
{
public:
        char* name;
        int age;
        CStest(){};
        CStest(CStest& cst) //这就是拷贝构造函数,实现复制。
        {

                this->name = new char(strlen(cst.name) + 1);
                strcpy(name, cst.name);
                this->age = cst.age;
        }
        CStest(char* t_name,int t_age)
        {

                name = new char(strlen(t_name) + 1);
                strcpy(name, t_name);
                this->age = t_age;
        }
        ~CStest()        //这是析构函数,用于内存的释放delete.
        {
                if (name)
                {
                        delete name;
                }
        }
protected:
private:
};

void test()        //定义函数,创造对象,并赋值。
{
        /*CStudent stu1 = {"sunguofu",13};*/
        CStest test1 = { "sunguofu", 13 }, test2;
        test2 = test1;
}

int _tmain(int argc, _TCHAR* argv[])
{
        test();        //调用函数,当函数执行后就报销。
        return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以前帖子的方法也测试过,还是不行的。
        CStest(CStest& cst)
        {
                int dst_name_len = 0;
                if (cst.name)
                {
                        dst_name_len = strlen(cst.name );
                }
       
               
                        name = 0;
               
                if (dst_name_len>0)
                {
                        this->name = new char(dst_name_len+1);
                        memset(name, 0, dst_name_len);
                        strcpy(name, cst.name);
                }
                this->age = cst.age;
        }
最佳答案
70_avatar_small
2021-3-12 09:43:43
本帖最后由 tony666 于 2021-3-12 09:51 编辑

你这调的是operator= 不是拷贝构造函数 ,拷贝构造函数是 “构造函数”
  1. CStest test1 = { (char*)"sunguofu", 13 }, test2(test1);
复制代码

另外this->name = new char[strlen(cst.name) + 1];
new 数组用中括号




上一篇:关于c++编写exe小程序
下一篇:关于shellcode调用崩溃的问题
70_avatar_middle
最佳答案
49 
在线会员 发表于 2021-3-12 09:43:43 | 显示全部楼层    本楼为最佳答案   
bestAnswer
本帖最后由 tony666 于 2021-3-12 09:51 编辑

你这调的是operator= 不是拷贝构造函数 ,拷贝构造函数是 “构造函数”
  1. CStest test1 = { (char*)"sunguofu", 13 }, test2(test1);
复制代码

另外this->name = new char[strlen(cst.name) + 1];
new 数组用中括号
88_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-3-12 09:37:42 | 显示全部楼层
以前帖子的方法也测试过,结果还是不行的。
88_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-3-12 10:29:07 | 显示全部楼层
本帖最后由 sunguofu8 于 2021-3-12 10:39 编辑

感谢 tony666 的指点,确定是[]的问题所致,已解决!再次感谢!
this->name = new char[dst_name_len+1];
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

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

关闭

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

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

GMT+8, 2023-9-28 02:47

Powered by CcTry.CoM

© 2009-2021 cctry.com

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