Is this copy initialization or direct initialization ?

S

subramanian100in

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
 
A

amrollahi.saeed

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
 
A

amrollahi.saeed

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
 
Joined
Dec 30, 2008
Messages
2
Reaction score
0
Test another = obj; //copy-initialization
Test another(obj); //copy-initialization
another = obj; //direct -initialization
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top