Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > value & default initialization, and copy initialization

Reply
Thread Tools

value & default initialization, and copy initialization

 
 
Taras_96
Guest
Posts: n/a
 
      10-29-2009
Hi all,

I was hoping to run my understanding of the concepts in the subject
line past the news group. Given the class:

class Foo
{
public:
Foo(int);
Foo();
Foo (Foo const &);
};

Using the excerpts from the standard:

“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. Otherwise, if no initializer is specified for a
nonstatic object, the object and its subobjects, if any, have an
indeterminate initial value"

and:

"An object whose initializer is an empty set of parentheses, i.e., (),
shall be value-initialized."

Is the following correct?

Foo foo; // default initialised
Foo foo(); // value initialised
Foo foo = Foo(); // temporary Foo is value initialised, then foo is
copy-initialized with temporary. Note that compiler may optimise away
copy, but it DOES require Foo to be copy constructible. ie: it is
equiv to Foo foo(Foo());

int x; // unitialized
int x = int(); // temporary is value initialised, then x is copy-
initialized with temporary. Note that compiler may optimise away copy

Foo* pfoo = new Foo; // a Foo object is default initialised, and then
the pfoo pointer is copy initialized with the return value of the new
operator
Foo* pfoo = new Foo(); // a Foo object is value initialised, and then
the pfoo pointer is copy initialized with the return value of the new
operator

int* pInt = new int(); // an int is value initialised, and then the
pInt pointer is copy initialized with the return value of the new
operator
int* pInt = new int; // an int is uninitialised, and then the pInt
pointer is copy initialized with the return value of the new operator.
*pInt is indeterminate

Foo foo = 5; // equivalent to Foo foo = Foo(5); temporary Foo object
constructed, and then foo is copy initialized with temporary. Note
that the compiler may optimize away the copy
which of course is the same as
Foo foo(Foo(5));

Cheers!

Taras
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-29-2009
Taras_96 wrote:

> Hi all,
>
> I was hoping to run my understanding of the concepts in the subject
> line past the news group. Given the class:
>
> class Foo
> {
> public:
> Foo(int);
> Foo();
> Foo (Foo const &);
> };
>

[...]
> Is the following correct?


> Foo foo; // default initialised
> Foo foo(); // value initialised
> Foo foo = Foo(); // temporary Foo is value initialised, then foo is
> copy-initialized with temporary. Note that compiler may optimise away
> copy, but it DOES require Foo to be copy constructible. ie: it is
> equiv to Foo foo(Foo());


Just one remark not relating to initialization: the line

Foo foo();

does not define a Foo object named foo, but declares a function foo()
without arguments and return type Foo.

And another remark about value initialization and default initialization: in
the case of the given class, I think, there is no observable difference. For
objects of class type with a user-declared default constructor, value
initialization just calls the default constructor (which has to be
accessible).


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Taras_96
Guest
Posts: n/a
 
      10-29-2009
Hi Kai,

On Oct 29, 7:46*pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:

> Just one remark not relating to initialization: the line
>
> * Foo foo();
>
> does not define a Foo object named foo, but declares a function foo()
> without arguments and return type Foo.


Yes of course, I knew that :*).
>
> And another remark about value initialization and default initialization: in
> the case of the given class, I think, there is no observable difference. For
> objects of class type with a user-declared default constructor, value
> initialization just calls the default constructor (which has to be
> accessible).


I believe that value & default initializations are slightly different,
see: http://groups.google.com/group/comp....4a1bb3d44a8690.

Does everything else seem correct?

Taras
 
Reply With Quote
 
Michael Tsang
Guest
Posts: n/a
 
      10-30-2009
Default initialization means for non-class type, no initialization, for
class type, call the default constructor.

Value initialization means always call the default constructor, even for
non-class types, which are zero-initialized.

Copy initialization means calling the copy constructor, which is equal to
memcpy for POD-types.
 
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
copy initialization and direct initialization from C++ Primer pauldepstein@att.net C++ 5 03-26-2009 06:32 PM
Is this copy initialization or direct initialization ? subramanian100in@yahoo.com, India C++ 3 12-30-2008 12:57 PM
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 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