By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,511 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,511 IT Pros & Developers. It's quick & easy.

The default constructor

P: n/a
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!
Oct 28 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* 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();
}
--
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?
Oct 28 '05 #2

P: n/a
Thank you!

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* 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();
}
--
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?

Oct 28 '05 #3

P: n/a
Alf P. Steinbach wrote:
* 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();
}


Isn't his default constructor also private by default?
Oct 28 '05 #4

P: n/a
On Fri, 28 Oct 2005 03:54:23 GMT, red floyd <no*****@here.dude> wrote:

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.
Oct 28 '05 #5

P: n/a
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?


"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* 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();
}
--
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?

Oct 28 '05 #6

P: n/a
shikn wrote:

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:
For a constant you have to _explicitly_ specify the value. That can be
through a user-defined default constructor, or ...


--
Karl Heinz Buchegger
kb******@gascad.at
Oct 28 '05 #7

P: n/a
Thanks a lot

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:43***************@gascad.at...
shikn wrote:

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:
For a constant you have to _explicitly_ specify the value. That can be through a user-defined default constructor, or ...


--
Karl Heinz Buchegger
kb******@gascad.at

Oct 28 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.