|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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, |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
utab wrote:
> [..] > 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 -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 8, 8:35 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> utab wrote: > > [..] > > 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. |
|
![]() |
| Outils de la discussion | |
|
|