default constructor

R

Rahul

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!!!
 
S

Saeed Amrollahi

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
 
A

Abhishek Padmanabh

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". :)
 
T

terminator

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.




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top