Topic: const objects and default constructors


Author: "ThosRTanner" <ttanner2@bloomberg.net>
Date: Wed, 18 Jan 2006 09:30:07 CST
Raw View
Has the requirement for const objects to have default constructors been
changed in the latest draft? I'm not sure where to look to check this.

Our compiler complains about the following:

class Spog { private: int a; public: Spog() : a(0) {} };

class Wurble { private: Spog b; public: const void wheep() { const Spog
b; } };
//OK so far

class Drivel { const void splog() { const Wurble a;} };
//Warns const object a requires a user-declared default constructor.

Why? I can see there would be problems if Spog didn't have a default
constructor (so Spog::a took a random value, which I presume it might
well do), but I don't see under the circumstances why class Drivel
should merit that warning. all the member variables in Wurble have user
declared default constructors.

Many thanks

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Wed, 18 Jan 2006 21:39:33 GMT
Raw View
ThosRTanner wrote:
> Has the requirement for const objects to have default constructors been
> changed in the latest draft?

First of all, since 1997 we have an internationally approved _Standard_,
not a draft.  We actually have another edition of the Standard since 2003.
And, no, the latest one does not change the previous one's requirement for
a non-POD class to have an explicitly defined default constructor if you
want to declare/define a const object.

 > I'm not sure where to look to check this.

8.5/9

> Our compiler complains about the following:
>
> class Spog { private: int a; public: Spog() : a(0) {} };
>
> class Wurble { private: Spog b; public: const void wheep() { const Spog
> b; } };
> //OK so far
>
> class Drivel { const void splog() { const Wurble a;} };
> //Warns const object a requires a user-declared default constructor.

Rightfully so.  8.5/9 requires that if no initialiser is given (like in
your case here with 'a') the object shall be default-initialised, and if
the object is 'const', the underlying class shall have a user-defined
default constructor.

> Why?

Because otherwise the compiler doesn't know what to do.  You need to be
explicit.

 > I can see there would be problems if Spog didn't have a default
> constructor (so Spog::a took a random value, which I presume it might
> well do), but I don't see under the circumstances why class Drivel
> should merit that warning. all the member variables in Wurble have user
> declared default constructors.

I would venture a guess that it's done to simplify development of C++
compilers.  They don't have to look deeper than _this_class_ to see if
how to complete the initialisation.

V

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Thu, 19 Jan 2006 00:35:51 GMT
Raw View
Victor Bazarov wrote:
> And, no, the latest one does not change the previous one's requirement for
> a non-POD class to have an explicitly defined default constructor if you
> want to declare/define a const object.

If you want to define a const object *and* you don't provide an
initializer, to be precise.

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]