The default constructor

S

shikn

I wrote one simple piece of codes to test const objects in C++ as below:

=================================
class Empty{};

main()
{
const Empty e1;
}
==================================


I built this with g++(version 3.2.2 20030222), the compiling result is:
======================================
$ g++ empty.C
empty.C: In function `int main()':
empty.C:5: uninitialized const `e1'
======================================

Many books on C++ said compiler shall automatically generate one default
constructor
for one class if there is no constructor in codes, so the statement "const
Empty e1"
should invoke the constructor that compiler generates for the class.

But why the compiler still reports "uninitialized const "?

If I change the statement as this: const Empty e1( ), the compiling shall
pass.

Thanks in advance!
 
A

Alf P. Steinbach

* shikn:
I wrote one simple piece of codes to test const objects in C++ as below:

=================================
class Empty{};

main()
{
const Empty e1;
}

Many books on C++ said compiler shall automatically generate one default
constructor for one class if there is no constructor in codes, so the
statement "const Empty e1" should invoke the constructor that compiler
generates for the class.

Nope.

But why the compiler still reports "uninitialized const "?

For a constant you have to _explicitly_ specify the value. That can be
through a user-defined default constructor, or by explictly invoking
default-construction. You haven't, so the compiler rightly complains.

If I change the statement as this: const Empty e1( ), the compiling shall
pass.

Oops, that's not constant declaration, that's a declaration of a
function e1 with no arguments, with result type 'Empty const'.

To invoke the default-construction explicitly, do

class Empty {};

int main()
{
Empty const e1 = Empty();
}
 
S

shikn

Thank you!

Alf P. Steinbach said:
* shikn:

For a constant you have to _explicitly_ specify the value. That can be
through a user-defined default constructor, or by explictly invoking
default-construction. You haven't, so the compiler rightly complains.



Oops, that's not constant declaration, that's a declaration of a
function e1 with no arguments, with result type 'Empty const'.

To invoke the default-construction explicitly, do

class Empty {};

int main()
{
Empty const e1 = Empty();
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
R

red floyd

Alf said:
* shikn:



For a constant you have to _explicitly_ specify the value. That can be
through a user-defined default constructor, or by explictly invoking
default-construction. You haven't, so the compiler rightly complains.





Oops, that's not constant declaration, that's a declaration of a
function e1 with no arguments, with result type 'Empty const'.

To invoke the default-construction explicitly, do

class Empty {};

int main()
{
Empty const e1 = Empty();
}

Isn't his default constructor also private by default?
 
Z

Zara

Isn't his default constructor also private by default?

No. Default contructor, default copy operator, default copy assignment
are public. It they were private, there would be unusable.
 
S

shikn

It is strange that
if I change to following codes

class Empty{
public: Empty(){};
};

int main(int argc, char** argv)
{
const Empty e0;
......
}

Although e0 has no initialize arguments, it can pass compilation.

But how to interpret this?
 
K

Karl Heinz Buchegger

shikn said:
It is strange that
if I change to following codes

class Empty{
public: Empty(){};
};

int main(int argc, char** argv)
{
const Empty e0;
.....
}

Although e0 has no initialize arguments, it can pass compilation.

But how to interpret this?

To quote Alf:
 

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
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top