Is there a difference between this copy and direct initialization

S

subramanian100in

Suppose 'Test' is a user-defined class which has a default ctor and a
copy ctor(which is not private and 'explicit') defined.

Suppose I have
Test obj;

Test copy_init = obj;
Is this copy-initialization ?

Test direct_init(obj);

Is there a difference between these two initializations ?
Which form should be preferred and advisable ?

Kindly explain.

Thanks
V.Subramanian
 
Y

yqever

Test obj; // constructor
Test obj2 = obj; // copy constructor
Test obj3(obj); // copy constructor
Test obj4;
obj4 = obj; // operator =
 
A

Alf P. Steinbach

* (e-mail address removed), India:
Suppose 'Test' is a user-defined class which has a default ctor and a
copy ctor(which is not private and 'explicit') defined.

Suppose I have
Test obj;

Test copy_init = obj;
Is this copy-initialization ?

By definition. The term refers to the syntax.

Test direct_init(obj);

This is called direct initialization (syntax).

Is there a difference between these two initializations ?

Not for the example you've shown. But in other cases yes. E.g.

Test o = "duh";

may create a temporary Test and copy that, while

Test o( "duh" );

doesn't.

Which form should be preferred and advisable ?

I generally prefer the "=" because it's more clear.

For the novice it also avoids the dreaded so called "most vexing parse" where
the compiler treats an attempted variable declaration as a routine declaration.

But in some cases you can't use "=", because it requires an accessible copy
constructor. For example, you can't initialize a std::eek:stringstream that way,
since streams are not copyable. Conversely, in a great many cases you can't use
direct initialization syntax, e.g. it's not supported for specififying a default
for a formal argument.

So it's not possible to choose a single syntax to use everywhere.

At least until C++0x (but I don't think even C++0x will make that possible).


Cheers & hth.,

- Alf
 
J

Juha Nieminen

Alf said:
Not for the example you've shown. But in other cases yes. E.g.

Test o = "duh";

may create a temporary Test and copy that, while

Test o( "duh" );

doesn't.

There is another important difference: The former requires that 'Test'
have an accessible copy constructor, while the latter doesn't. And this
even if the compiler never calls that copy constructor.
 
A

Alf P. Steinbach

* Juha Nieminen:
There is another important difference: The former requires that 'Test'
have an accessible copy constructor, while the latter doesn't. And this
even if the compiler never calls that copy constructor.

I believe I discussed that in the part you snippeth:

"But in some cases you can't use "=", because it requires an accessible copy
constructor" ... example.


Cheers,

- Alf
 
J

James Kanze

* (e-mail address removed), India:
By definition. The term refers to the syntax.
This is called direct initialization (syntax).
Not for the example you've shown. But in other cases yes. E.g.
Test o = "duh";
may create a temporary Test and copy that, while

May, but it isn't required to, and none of the compilers I know
do in fact create the temporary. (It is required that their be
an accessible copy constructor, however, even if it isn't used.)
Test o( "duh" );
I generally prefer the "=" because it's more clear.

Yes. It makes it clear that there is an assignment (that the
operator=) is used:).

Seriously, I don't think that there is a "clearer" solution.
Both have problems. I generally use copy-initialization when
non-class types are involved, direct-initialization otherwise.
For the novice it also avoids the dreaded so called "most
vexing parse" where the compiler treats an attempted variable
declaration as a routine declaration.
But in some cases you can't use "=", because it requires an
accessible copy constructor. For example, you can't initialize
a std::eek:stringstream that way, since streams are not copyable.

In a lot of cases, you can't use the copy-initialization format,
because it only allows a single argument (unless you explicitly
construct on the right side of the =). I use
direct-initialization for class types because the syntax reminds
me of calling the constructor (which is what actually happens).
I use copy-initialization for other types because that's what
I'm used to, going back to my C programming days. And because
at least one early C++ compiler choked on direct-initialization
for some basic types. (But those are very much purely personal
preferences.)
Conversely, in a great many cases you can't use direct
initialization syntax, e.g. it's not supported for
specififying a default for a formal argument.

That's a different case---you're specifying a default argument
for a single parameter, not for the object. It's not
"initialization", at least not in the sense the standard uses
the term.
So it's not possible to choose a single syntax to use
everywhere.

Yes and no. If you always enclose any arguments in an
additional set of parentheses (so they can't be interpreted as
parameters of a function), then you can use
direct-initialization for most types. But not for aggregate
initialization, e.g.:
int array[] = { 1, 2, 5 } ;
At least until C++0x (but I don't think even C++0x will make
that possible).

C++0x adds braced-list-initialization, which is also
direct-initialization (unless there is an = sign before the
braced list, in which case, it is copy initialization), but with
a syntax which should avoid the most difficult parse problem.
But I'm not sure that it can always be used:

std::vector< size_t > v1( 42, 1 ) ;
std::vector< size_t > v2{ 42, 1 } ;

The first creates a vector with 42 elements, all initialized
with 1. IIUC, the second creates a vector with two elements,
the first of which is 42, and the second 1. Other than writing
out 42 1's, I don't think that there's any way of getting the
effect of the first constructor with list 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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top