Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default ctor and derived classes

Reply
Thread Tools

Default ctor and derived classes

 
 
utab
Guest
Posts: n/a
 
      02-08-2008
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 derivedublic 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,
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-08-2008
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


 
Reply With Quote
 
 
 
 
Martin York
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 8:35 am, "Victor Bazarov" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM



Advertisments