Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Ways of constructing objects.

Reply
Thread Tools

Ways of constructing objects.

 
 
Robbie Hatley
Guest
Posts: n/a
 
      07-04-2004
A few days ago, tom_usenet <> wrote
in a message in this group, concerning ways of constructing
objects:

> > int main(void)
> > {
> > MyStruct Blat; // Zeros members; calls only one constructor
> > }

>
> Yup, that's the best plan, but in generic code where you want default
> initialization but don't know the type, you should probably do:
>
> T t = T();


So, Tom (or anyone who knows something about this issue):
You're saying I should construct objects like this:

MyType Blat = MyType();

Instead of:

MyType Blat;

What's the difference? Why would the former provide
"default initialization" but the latter not?

As for "in generic code", I tried using former form
inside a template class and got the error message
"ISO C++ forbids initialization of member Blat".
But the latter form worked OK.

So I'm just wondering what this "T t = T()" business
is all about.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* "Robbie Hatley" <lonewolfintj at pacbell dot net>:
> A few days ago, tom_usenet <> wrote
> in a message in this group, concerning ways of constructing
> objects:
>
> > > int main(void)
> > > {
> > > MyStruct Blat; // Zeros members; calls only one constructor
> > > }

> >
> > Yup, that's the best plan, but in generic code where you want default
> > initialization but don't know the type, you should probably do:
> >
> > T t = T();

>
> So, Tom (or anyone who knows something about this issue):
> You're saying I should construct objects like this:
>
> MyType Blat = MyType();
>
> Instead of:
>
> MyType Blat;
>
> What's the difference? Why would the former provide
> "default initialization" but the latter not?
>
> As for "in generic code", I tried using former form
> inside a template class and got the error message
> "ISO C++ forbids initialization of member Blat".
> But the latter form worked OK.
>
> So I'm just wondering what this "T t = T()" business
> is all about.


§8.5/9, effectively, the form "T t = T" does not initialize t when
T is a non-static POD (in the 1997 standard non-static was implied,
in the 2003 standard, Technical Corrigendum 1, it is explicit).

There is a difference between 1997 and 2003 standard regarding
"T t = T()". 1997 $8.5/7: default-initialization. 2003: "value-
initialization".

Andrew Koenig was the man, and he has explained this subtle change
very clearly a number of times in this group; unfortunately my brain
does not seem able to retain the clear understanding I always have
after reading Andrew's explanations, and the definition in the HS
involves phrasing such as "the constructor ... is called" (which
invariably makes a swarm of buzzing flies descend on me when I use it
in this newsgroup), and I'm too lazy to Google, so, I'll pass...

--
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?
 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      07-04-2004

"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message
news:40e78441$0$5859$ om...
> A few days ago, tom_usenet <> wrote
> in a message in this group, concerning ways of constructing
> objects:
>
> > > int main(void)
> > > {
> > > MyStruct Blat; // Zeros members; calls only one constructor
> > > }

> >
> > Yup, that's the best plan, but in generic code where you want

default
> > initialization but don't know the type, you should probably do:
> >
> > T t = T();

>


You can find a useful discussion and some references here:
http://www.boost.org/libs/utility/value_init.htm.

Jonathan


 
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
concurrency constructing objects Archimede Java 5 12-01-2005 11:42 AM
Constructing SOAP Message gabsaga_tata@hotmail.com Java 3 08-29-2005 08:00 PM
access webcontrol dynamically from code behind - by constructing name Leo Muller ASP .Net 2 06-16-2004 01:16 PM
JDBC - Constructing a query realtime Mohammad S Khan Java 0 10-31-2003 12:03 PM
Re: constructing an object from bytes rather than a constructor Mike Wahler C++ 3 08-01-2003 01:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57