PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Default ctor and derived classes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Default ctor and derived classes

Réponse
 
LinkBack Outils de la discussion
Vieux 08/02/2008, 16h10   #1
utab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Default ctor and derived classes

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,
  Réponse avec citation
Vieux 08/02/2008, 16h35   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Default ctor and derived classes

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


  Réponse avec citation
Vieux 08/02/2008, 18h05   #3
Martin York
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Default ctor and derived classes

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h59.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10507 seconds with 11 queries