 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
shikn Guest
|
Posted: Fri Oct 28, 2005 2:16 am Post subject: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 2:37 am Post subject: Re: The default constructor |
|
|
* 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
|
Posted: Fri Oct 28, 2005 2:48 am Post subject: Re: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 3:54 am Post subject: Re: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 4:39 am Post subject: Re: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 7:49 am Post subject: Re: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 8:47 am Post subject: Re: The default constructor |
|
|
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
|
Posted: Fri Oct 28, 2005 10:45 am Post subject: Re: The default constructor |
|
|
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 |
|
 |
|
|
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
|
|