![](/img/trans.png)
[英]Overloading copy constructor with delete and then calling the default constructor of a subclass in C++
[英]am I forced to use/create the default super constructor when creating a subclass copy constructor? [C++]
我正在尝试在 C++ 中创建超类的子类,而不使用任何默认构造函数(我不需要它们)。
以下是课程:
Object.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_ */
Object.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;
}
Class.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_ */
Class.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:错误:没有匹配的 function 用于调用“Class::Class()”
如果我为 Class 而不是为 Object 创建无用的默认构造函数,我将收到相同的错误消息:
没有匹配的 function 调用 'Object::Object()'
而且我不想要那些空的(在我的情况下)默认构造函数。 有没有解决的办法?
将此作为答案发布以提高知名度。
鉴于您的任何数据都不是在运行时生成的(没有动态资源),您可以充分利用编译器提供的复制构造函数和析构函数。
复制/移动构造函数和析构函数的存在是为了解决您没有的问题。 我建议在 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
的默认构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.