Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this copy initialization or direct initialization ?

Reply
Thread Tools

Is this copy initialization or direct initialization ?

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      12-30-2008
Suppose 'Test' is a class.

Suppose I create an instance 'obj' of the 'Test' class:

Test obj;

Now, suppose I write

Test another = obj;

My question: is this last line called 'direct -initialization' or
'copy-initialization' ?

Kindly clarify.

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
amrollahi.saeed@gmail.com
Guest
Posts: n/a
 
      12-30-2008
On Dec 30, 9:27*am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> Suppose 'Test' is a class.
>
> Suppose I create an instance 'obj' of the 'Test' class:
>
> Test obj;
>
> Now, suppose I write
>
> Test another = obj;
>
> My question: is this last line called 'direct -initialization' or
> 'copy-initialization' ?
>
> Kindly clarify.
>
> Thanks
> V.Subramanian


Hi

AFAIK, there isn't "direct -initialization" terminology in standard C+
+. I think the term "copy-initialization"
isn't standard too. What is clear is, in the following statements:
Test obj;
Test another = obj;
first an object (obj) is created using default constructor, then
another object (another) is created using
copy constructor. The second one is called "initialization", because
another is initialized using obj.

Cheers,
Saeed Amrollahi
 
Reply With Quote
 
 
 
 
amrollahi.saeed@gmail.com
Guest
Posts: n/a
 
      12-30-2008
On Dec 30, 9:27*am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> Suppose 'Test' is a class.
>
> Suppose I create an instance 'obj' of the 'Test' class:
>
> Test obj;
>
> Now, suppose I write
>
> Test another = obj;
>
> My question: is this last line called 'direct -initialization' or
> 'copy-initialization' ?
>
> Kindly clarify.
>
> Thanks
> V.Subramanian


Hi

I am sorry for some inaccuracy. My answer is not incorrect in general,
but I read the
C++ standard draft ISO/IEC JTC 1/SC 22 N 4411 and I found the
following statements:

The initialization that occurs in the form
T x = a;
as well as in argument passing, function return, throwing an
exception, handling an exception,
and aggregate member initialization is called copy-initialization.
The initialization that occurs in in the forms
T x(a);
T x{a};
as well as in new expressions, static_cast expressions, functional
notation type conversions, and base and
member initializers is called direct-initialization.
So I think the statement:
Test another = obj;
should be copy-initialization.

Cheers,
Saeed Amrollahi
 
Reply With Quote
 
prashanta prashanta is offline
Junior Member
Join Date: Dec 2008
Location: Bangalore
Posts: 2
 
      12-30-2008
Test another = obj; //copy-initialization
Test another(obj); //copy-initialization
another = obj; //direct -initialization
 
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
value & default initialization, and copy initialization Taras_96 C++ 3 10-30-2009 10:51 AM
Is there a difference between this copy and direct initialization subramanian100in@yahoo.com, India C++ 5 08-21-2009 09:02 AM
copy initialization and direct initialization from C++ Primer pauldepstein@att.net C++ 5 03-26-2009 06:32 PM
copy vs direct initialization subramanian100in@yahoo.com, India C++ 2 11-23-2007 08:45 PM
direct vs copy initialization error subramanian100in@yahoo.com, India C++ 3 07-03-2007 05:44 AM



Advertisments