Paiku Han
Paiku Han

Reputation: 583

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

I'm trying to create subclasses of superclasses in C++ without using any default constructor (I don't need them).

Here are the classes:

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;
}

Subclass.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_ */

Subclass.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;
}

Whenever I try to compile the code with a scalar instance and a Pointer instance of Subclass I get an error like this:

..\sources\Subclass.cpp: In copy constructor 'Subclass::Subclass(const Subclass&)':

..\sources\Subclass.cpp:17:41: error: no matching function for call to 'Class::Class()'

and if I create the useless default constructor for Class but not for Object I will get the same error saying:

no matching function for call to 'Object::Object()'

And I don't want those empty (in my case) default constructors. Is there a way around this?

Upvotes: 0

Views: 74

Answers (3)

sweenish
sweenish

Reputation: 5202

Posting this as an answer for more visibility.

Given that none of your data is generated at runtime (no dynamic resources), you will be fine taking advantage of the compiler-provided copy constructors and destructors.

Copy/Move constructors and destructors exist to solve a problem you don't have. I recommend reading this page on cppreference.com that was also linked by @Marek R.

If they must be explicitly declared to satisfy some arbitrary requirement, they can be declared as default.

Example:

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

Upvotes: 1

Jarod42
Jarod42

Reputation: 217235

Your copy constructors should be:

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

and

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

or even, in your case:

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

or simply omit them in declaration.

Omitting : Object(other) in initializer list is equivalent to

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

Which is invalid as no default constructor for Object.

Upvotes: 0

Marek R
Marek R

Reputation: 37657

Read about "The rule of three/five/zero".

Basically since your copy constructor does what default does and you destructor are useless "The rule of zero" should do the job for you.

Just trash explicit declarations and definitions of copy constructor and destructor.

Upvotes: 0

Related Questions