Default ctor and derived classes

U

utab

Dear all,

I was experimenting on inheritance and I did some simple tries on a
simple class. See the code below:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Base{
//Base(int val):a(val){std::cout << "Ctor :" << a << std::endl;}
//line void print(){std::cout << id << std::endl;}

void print(){std::cout << str << '\n' ;}
//private:
protected:
std::string str;
std::vector<int> vec;
int a;
};

struct derived:public Base{
derived(int val): a(val){ }
private:
int a;
};

int main(){

//Base b(4);
Base b;
derived d(6);
b.print();
return 0;

}


This code just outputs an empty line. That is fine.

When I uncomment the line in the base class for the ctor(for Base)
definition and the related declaration in the main function(plus
commenting out Base b in the main), I am getting an error related to
the default ctor:

simple.cc: In constructor 'derived::derived(int)':
simple.cc:20: error: no matching function for call to 'Base::Base()'
simple.cc:8: note: candidates are: Base::Base(int)
simple.cc:7: note: Base::Base(const Base&)

My question is that when there is a ctor other than the default ctor,
do I have to explicitly define the default ctor? Or am I missing sth
important in the context of this simple inheritance case study?

My best,
 
V

Victor Bazarov

utab said:
[..]
My question is that when there is a ctor other than the default ctor,
do I have to explicitly define the default ctor?
Yes.

Or am I missing sth
important in the context of this simple inheritance case study?

Sth important is that if you don't declare/define *any* constructor,
the default one is declared/defined for you (if possible). If you
do declare/define *any* constructor, the compiler no longer declares
or defines the default c-tor.

V
 
M

Martin York

utab said:
[..]
My question is that when there is a ctor other than the default ctor,
do I have to explicitly define the default ctor?
Yes.

Or am I missing sth
important in the context of this simple inheritance case study?

Sth important is that if you don't declare/define *any* constructor,
the default one is declared/defined for you (if possible).

Note. This has nothing to do with inheritance. It is just a property
of ALL classes in C++.

Also note that the constructor is not the only auto generated method
if nothing is supplied.

Auto generated methods:
Constructor: Will call the default constructor of all members.
Destructor: All members destructor's are called. (but does
nothing else)
Copy Constructor: Uses each members copy constructor to copy the
member variable across to the new object.
Assignment op Uses each members assignment operator to copy the
member variables across to the lhs object.


Be careful when you have raw pointers in an object and the object
manages the allocation/deallocation of memory for those pointers, as
the default methods may not play nicely.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top