|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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!!! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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!!! |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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". ![]() |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|