如何在C ++中同时使用默认和自定义副本构造函数? [英] How to use both default and custom copy constructor in C++?

查看:83
本文介绍了如何在C ++中同时使用默认和自定义副本构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的课程很长,有很多数据成员.我想为此编写一个副本构造函数.但是,如果我编写自己的副本构造函数,则会失去对默认副本构造函数的访问权限.

I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor.

我只想在自己的副本构造函数中修复一些指针.所以我想拥有一个对象的浅表副本,可以通过默认的副本构造函数来完成.

I just want to repair a few pointers in my own copy constructor. So I want to have a shallow copy of the object which can be done by the default copy constructor.

当我拥有自己的副本构造函数时,是否可以访问默认的副本构造函数?

Is there a possibility to access the default copy constructor when I have my own copy constructor?

推荐答案

将您不想更改的内容包装在一个结构中,并从中(私下)派生.在您的复制构造函数中,只需调用基类的复制构造函数即可.

Wrap the things you don't want to change in a struct, and derive (privately) from it. In your copy constructor, simply invoke the copy constructor of your base class.

这篇关于如何在C ++中同时使用默认和自定义副本构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆