473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!
Oct 28 '05 #1
7 2561
* 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
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...
12
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() {} };
18
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...
10
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...
19
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...
12
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;
10
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...
74
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...
23
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...
4
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.