创建子类复制构造函数时是否强制使用/创建默认的超级构造函数?[C++]

am I forced to use/create the default super constructor when creating a subclass copy constructor? [C++]

本文关键字:构造函数 创建 C++ 默认 复制 子类 是否      更新时间:2023-10-16

我正在尝试在不使用任何默认构造函数的情况下C++中创建超类的子类(我不需要它们(。

以下是课程:

对象.hpp

#ifndef OBJECT_HPP_
#define OBJECT_HPP_
class Object {
public:
//constructors.
Object(int s, int t);
Object(const Object& other);
//destructor
~Object();
//getters
int getS();
int getT();
//setters
void setS(int s);
void setT(int t);
private:
int s, t;
};
#endif /* OBJECT_HPP_ */

对象.cpp

#include "../headers/Object.hpp"
//constructors.
Object::Object(int s, int t){
this->s = s;
this->t = t;
}
Object::Object(const Object& other){
s = other.s;
t = other.t;
}
//destructor
Object::~Object(){
s = 0;
t = 0;
}
//getters
int Object::getS(){
return s;
}
int Object::getT(){
return t;
}
//setters
void Object::setS(int s){
this->s = s;
}
void Object::setT(int t){
this->t = t;
}

类.hpp

#ifndef CLASS_HPP_
#define CLASS_HPP_
#include "Object.hpp"
class Class: public Object {
public:
//constructors.
Class(int x, int y, int z);
Class(const Class& other);
//destructor
~Class();
//getters
int getX();
int getY();
int getZ();
//setters
void setX(int x);
void setY(int y);
void setZ(int z);
private:
int x, y, z;
};
#endif /* CLASS_HPP_ */

类.cpp

#include "../headers/Class.hpp"
//constructors.
Class::Class(int x, int y, int z) : Object(x, y){
this->x = x;
this->y = y;
this->z = z;
}
Class::Class(const Class& other){
x = other.x;
y = other.y;
z = other.z;
}
//destructor
Class::~Class(){
x = 0;
y = 0;
z = 0;
//Object::~Object();
}
//getters
int Class::getX(){
return x;
}
int Class::getY(){
return y;
}
int Class::getZ(){
return z;
}
//setters
void Class::setX(int x){
this->x = x;
}
void Class::setY(int y){
this->y = y;
}
void Class::setZ(int z){
this->z = z;
}

子类.hpp

#ifndef SUBCLASS_HPP_
#define SUBCLASS_HPP_
#include "Class.hpp"
class Subclass: public Class {
public:
//constructors.
Subclass(int u, int v, int w);
Subclass(const Subclass& other);
//destructor
~Subclass();
//getters
int getU();
int getV();
int getW();
//setters
void setU(int u);
void setV(int v);
void setW(int w);
private:
int u, v, w;
};
#endif /* SUBCLASS_HPP_ */

子类.cpp

#include "../headers/Subclass.hpp"
//constructors.
Subclass::Subclass(int u, int v, int w) : Class(u, v, w){
this->u = u;
this->v = v;
this->w = w;
}
Subclass::Subclass(const Subclass& other){
u = other.u;
v = other.v;
w = other.w;
}
//destructor.
Subclass::~Subclass(){
u = 0;
v = 0;
w = 0;
//Class::~Class();
}
//getters
int Subclass::getU(){
return u;
}
int Subclass::getV(){
return v;
}
int Subclass::getW(){
return w;
}
//setters
void Subclass::setU(int u){
this->u = u;
}
void Subclass::setV(int v){
this->v = v;
}
void Subclass::setW(int w){
this->w = w;
}

每当我尝试使用标量实例和子类的指针实例编译代码时,我都会收到如下错误:

..\sources\Subclass.cpp: 在复制构造函数 'Subclass::Subclass(const Subclass&(' 中:

..\sources\Subclass.cpp:17:41:错误:调用"Class::Class(("没有匹配函数

如果我为类而不是对象创建无用的默认构造函数,我将收到相同的错误,说:

调用"对象::对象(("没有匹配函数

而且我不想要那些空的(在我的情况下(默认构造函数。有没有办法解决这个问题?

发布此内容作为答案以提高可见性。

鉴于您的任何数据都不会在运行时生成(没有动态资源(,则可以利用编译器提供的复制构造函数和析构函数。

复制/移动构造函数和析构函数的存在是为了解决您没有的问题。我建议在 cppreference.com 上阅读此页面,该页面也由@Marek R链接。

如果必须显式声明它们以满足某些任意要求,则可以将它们声明为默认值。

例:

// In Subclass.hpp
Subclass(const Subclass& other) = default;

复制构造函数应该是:

Class::Class(const Class& other) : Object(other)
{
x = other.x;
y = other.y;
z = other.z;
}

Subclass::Subclass(const Subclass& other) : Class(other)
{
u = other.u;
v = other.v;
w = other.w;
}

甚至,在您的情况下:

Class::Class(const Class& other) = default;
Subclass::Subclass(const Subclass& other) = default;

或者干脆在声明中省略它们。

在初始值设定项列表中省略: Object(other)等效

Class::Class(const Class& other) : Object(/*Empty*/) 
{
x = other.x;
y = other.y;
z = other.z;
}

这是无效的,因为没有Object的默认构造函数。

阅读"三/五/零法则"。

基本上,由于您的复制构造函数执行默认执行的操作,并且您的析构函数无用,"零规则"应该为您完成这项工作。

只是垃圾复制构造函数和析构函数的显式声明和定义。