Ways of constructing objects.

R

Robbie Hatley

in a message in this group, concerning ways of constructing
objects:
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
 
A

Alf P. Steinbach

* "Robbie Hatley said:
in a message in this group, concerning ways of constructing
objects:


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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top