C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The default constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
shikn
Guest





PostPosted: Fri Oct 28, 2005 2:16 am    Post subject: The default constructor Reply with quote



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!


Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Oct 28, 2005 2:37 am    Post subject: Re: The default constructor Reply with quote



* shikn:
Quote:
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.


Quote:
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.


Quote:
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?

Back to top
shikn
Guest





PostPosted: Fri Oct 28, 2005 2:48 am    Post subject: Re: The default constructor Reply with quote



Thank you!

"Alf P. Steinbach" <alfps (AT) start (DOT) no> wrote

Quote:
* 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?



Back to top
red floyd
Guest





PostPosted: Fri Oct 28, 2005 3:54 am    Post subject: Re: The default constructor Reply with quote

Alf P. Steinbach wrote:
Quote:
* 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?

Back to top
Zara
Guest





PostPosted: Fri Oct 28, 2005 4:39 am    Post subject: Re: The default constructor Reply with quote

On Fri, 28 Oct 2005 03:54:23 GMT, red floyd <no.spam (AT) here (DOT) dude> wrote:

Quote:

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.

Back to top
shikn
Guest





PostPosted: Fri Oct 28, 2005 7:49 am    Post subject: Re: The default constructor Reply with quote

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" <alfps (AT) start (DOT) no> wrote

Quote:
* 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?



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Oct 28, 2005 8:47 am    Post subject: Re: The default constructor Reply with quote

shikn wrote:
Quote:

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:

Quote:
For a constant you have to _explicitly_ specify the value. That can be
through a user-defined default constructor, or ...

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
shikn
Guest





PostPosted: Fri Oct 28, 2005 10:45 am    Post subject: Re: The default constructor Reply with quote

Thanks a lot

"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote

Quote:
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
[email]kbuchegg (AT) gascad (DOT) at[/email]



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.