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!!! 3 3748
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!!!
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". :)
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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() {}
};
|
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
|
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...
|
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
| |
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;
|
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...
|
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
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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. ...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |