473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!!!
Dec 9 '07 #1
3 3697
On Dec 9, 8:14 am, Rahul <sam_...@yahoo.co.inwrote:
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!!!
Dec 9 '07 #2
On Dec 9, 10:14 am, Rahul <sam_...@yahoo.co.inwrote:
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". :)
Dec 9 '07 #3
On Dec 9, 9:04 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 9, 10:14 am, Rahul <sam_...@yahoo.co.inwrote:


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.
Dec 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.