Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > anybody knows why?

Reply
Thread Tools

anybody knows why?

 
 
Yan
Guest
Posts: n/a
 
      12-03-2008
The ISO standard says (in 8.5 [dcl.init] paragraph 9):

If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the
object shall be default-initialized; if the object is of
const-qualified type, the underlying class type shall have a
user-declared default constructor.

Does anybody know what is the rationale behind forcing a programmer to
define a default constructor in this case? Why is it that the object
being of const type makes it a special case?

Thank you.
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-03-2008
On Dec 3, 3:50*pm, Yan <(E-Mail Removed)> wrote:
> The ISO standard says (in 8.5 [dcl.init] paragraph 9):
>
> If no initializer is specified for an object, and the object is of
> * * (possibly cv-qualified) non-POD class type (or array thereof), the
> * * object shall be default-initialized; if the object is of
> * * const-qualified type, the underlying class type shall have a
> * * user-declared default constructor.


I switched the order of your questions.

> Why is it that the object
> being of const type makes it a special case?


As a constant object can not be assigned to, it must be initialised
where it is declared.

> Does anybody know what is the rationale behind forcing a programmer to
> define a default constructor in this case?


The rationale is to avoid uninitialised constant objects.

Default initialisation of non-PODs does not involve default-
initialisation of POD members, so that they are left uninitialised.
The provided default constructor is supposed to initialise those
members.

--
Max
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      12-03-2008
On 2008-12-03 16:50, Yan wrote:
> The ISO standard says (in 8.5 [dcl.init] paragraph 9):
>
> If no initializer is specified for an object, and the object is of
> (possibly cv-qualified) non-POD class type (or array thereof), the
> object shall be default-initialized; if the object is of
> const-qualified type, the underlying class type shall have a
> user-declared default constructor.
>
> Does anybody know what is the rationale behind forcing a programmer to
> define a default constructor in this case? Why is it that the object
> being of const type makes it a special case?


Because if you have a class like this:

class Foo {
int i;
};

and then create a const instance of it:

const Foo foo;

and the language allowed it to use the compiler-generated default
constructor you would end up with Foo::i having a random value and no
way to change it. This is most probably not what you want, you probably
want to set Foo::i to some specific value, and then you need to do so in
the constructor. And if you really don't want to set Foo::i to some
specific value and use whatever value it gets, you can still do that
simply by not initialising Foo::i in the constructor.

--
Erik Wikström
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anybody knows the difference between onInit and PageLoad Andrea ASP .Net 1 03-29-2006 12:59 PM
Anybody knows if SQL2005 manager can managed App_data database? GS ASP .Net 0 01-10-2006 06:29 PM
Anybody knows NTsendmail and Perl??? Steve Perl 2 05-05-2004 06:25 PM
Is anybody who knows how to use DHCP, TFTP ... servers out of my uBR7114e? Oscar Cisco 0 01-08-2004 10:26 AM
PyGTK website is down. Anybody knows why and when it will be brought up? Hans Deragon Python 2 10-11-2003 04:42 PM



Advertisments