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 constructor
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
default constructor

Réponse
 
LinkBack Outils de la discussion
Vieux 09/12/2007, 05h14   #1
Rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut default constructor

Hi Everyone,

I have the following code and the compiler complains that there isn't
any default constructor available,

class C
{
private:
C()
{
printf("in private constructor of C\n");
}
};

class E : public C
{
}

E obj; // compile time error saying no default constructor
available in E.

However, it works fine if i change the constructor C() from private to
public access specification. I'm wondering why this is so?

I was thinking that default constructor is needed only in these
following cases,

1) a explicit constructor is provided
2) a custom constructor accepting parameters is provided

but i seem to be missing some other cases, can anyone point out that?

Thanks in advance!!!
  Réponse avec citation
Vieux 09/12/2007, 05h36   #2
Saeed Amrollahi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default constructor

On Dec 9, 8:14 am, Rahul <sam_...@yahoo.co.in> wrote:
> Hi Everyone,
>
> I have the following code and the compiler complains that there isn't
> any default constructor available,
>
> class C
> {
> private:
> C()
> {
> printf("in private constructor of C\n");
> }
>
> };
>
> class E : public C
> {
>
> }
>
> E obj; // compile time error saying no default constructor
> available in E.
>
> However, it works fine if i change the constructor C() from private to
> public access specification. I'm wondering why this is so?
>

Hi

According to the fundamental rules in C++:
1. When an object of derived class is created, its constructor should
call the constructor of immediate base class(es) using Initialization
list. In default construction, the base's default constructor is
called implicit.
2. The derived class has no priviledge for access to base class's
private members.
When obj is going to be created, the default constructor of C called
(implicitly), but it is private, and the no default constructor error
is issued.
You should place the C default constructor under public: or protected:
access control.

S. Amrollahi

> I was thinking that default constructor is needed only in these
> following cases,
>
> 1) a explicit constructor is provided
> 2) a custom constructor accepting parameters is provided
>
> but i seem to be missing some other cases, can anyone point out that?
>
> Thanks in advance!!!


  Réponse avec citation
Vieux 09/12/2007, 06h04   #3
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default constructor

On Dec 9, 10:14 am, Rahul <sam_...@yahoo.co.in> wrote:
> Hi Everyone,
>
> I have the following code and the compiler complains that there isn't
> any default constructor available,
>
> class C
> {
> private:
> C()
> {
> printf("in private constructor of C\n");
> }
>
> };
>
> class E : public C
> {
>
> }
>
> E obj; // compile time error saying no default constructor
> available in E.
>
> However, it works fine if i change the constructor C() from private to
> public access specification. I'm wondering why this is so?


It then works because now the default constructor of derived can "see"
atleast one base class constructor and that being the default one does
not need to change its initialization list to supply constructor
arguments.


> I was thinking that default constructor is needed only in these
> following cases,
>
> 1) a explicit constructor is provided
> 2) a custom constructor accepting parameters is provided
>
> but i seem to be missing some other cases, can anyone point out that?


Sorry, this does not make any sense to me. If you are asking why you
needed to provide a public default constructor for C to make E
instantiable. You could remove the constructor. The default one
provided by the compiler would be sufficient but it will not print "in
private constructor of C".
  Réponse avec citation
Vieux 09/12/2007, 11h31   #4
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default constructor

On Dec 9, 9:04 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
> On Dec 9, 10:14 am, Rahul <sam_...@yahoo.co.in> wrote:
>
>
>
>
>
> > Hi Everyone,

>
> > I have the following code and the compiler complains that there isn't
> > any default constructor available,

>
> > class C
> > {
> > private:
> > C()
> > {
> > printf("in private constructor of C\n");
> > }

>
> > };

>
> > class E : public C
> > {

>
> > }

>
> > E obj; // compile time error saying no default constructor
> > available in E.

>
> > However, it works fine if i change the constructor C() from private to
> > public access specification. I'm wondering why this is so?

>
> It then works because now the default constructor of derived can "see"
> atleast one base class constructor and that being the default one does
> not need to change its initialization list to supply constructor
> arguments.
>
> > I was thinking that default constructor is needed only in these
> > following cases,

>
> > 1) a explicit constructor is provided
> > 2) a custom constructor accepting parameters is provided

>
> > but i seem to be missing some other cases, can anyone point out that?

>
> Sorry, this does not make any sense to me. If you are asking why you
> needed to provide a public default constructor for C to make E
> instantiable. You could remove the constructor. The default one
> provided by the compiler would be sufficient but it will not print "in
> private constructor of C". - Hide quoted text -
>


for OP`s purpose a protected ctor for 'C' is better I guess:

class C
{
protected: //iheritable limited access
C()
{
printf("in private constructor of C\n");
}

};

C c;//Error:ctor not accessible.
E e;//OK: call C::C() .

alternatively 'E' can be a friend of 'C':

class C
{
private:
C()
{
printf("in private constructor of C\n");
}
friend class E;//access give special access privilages to 'class E'.
};

E e;//OK

regards,
FM.
  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 11h30.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14277 seconds with 12 queries