473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3772
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.padma n...@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
21201
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 pointers) and for objects types call their default constructor. Any others points i should know?
12
4768
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
3004
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 constructor that all parameters have default values
10
2407
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 zoot(1,2); It would instantiate MyClass, passing 1 and 2 to my constructor which in turn would do whatever I want it to. Would a default constructor be as follows:
19
6569
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 not declare one? 2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments? Thanks, Andrew
12
5818
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
4702
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 no-parameter constructor for my class with an empty function body. I then instantiated an object and tried printing its values, amazingly the members were already initialized. But how is this possible if I have not included any code for doing so. The...
74
16036
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
23
3663
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 the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
4
3714
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.