initialization problem

J

john sun

Hi,
I have a class with copy ctor and assignment operator.

MyClasss obj1(data);
MyClass obj2=obj1; in this case, the copy constructor is invoked instead of
assignment operator. I cannot understand why?

Thanks,

John
 
D

David Harmon

Hi,
I have a class with copy ctor and assignment operator.

MyClasss obj1(data);
MyClass obj2=obj1; in this case, the copy constructor is invoked instead of
assignment operator. I cannot understand why?

Because you are creating a new object. operator= applies when
assigning to an object that already exists. Pay no attention to the '='
character allowed largely for compatibility with C syntax. You may find
it more clear to use the first form only.
 
K

Karl Heinz Buchegger

john said:
Hi,
I have a class with copy ctor and assignment operator.

MyClasss obj1(data);
MyClass obj2=obj1; in this case, the copy constructor is invoked instead of
assignment operator. I cannot understand why?

Because the above is initialization. And initialization always means
that a constructor is invoked.

Initialization:
A new object is born and at the time of birth it is given a value

Assignment:
An already existing object gets a new value.
 
J

jeffc

john sun said:
Hi,
I have a class with copy ctor and assignment operator.

MyClasss obj1(data);
MyClass obj2=obj1; in this case, the copy constructor is invoked instead of
assignment operator. I cannot understand why?

It's a special case when you use the assignment symbol in the same statement
that you're defining/creating something.
 
V

Victor Bazarov

jeffc said:
instead

It's a special case when you use the assignment symbol in the same statement
that you're defining/creating something.

So, == is also a special case, right? Actually, when one stops
calling '=' "assignment symbol", it becomes easier, no _assignment_
to conflict with. :)
 
M

Martijn Lievaart

Because you are creating a new object. operator= applies when
assigning to an object that already exists. Pay no attention to the '='
character allowed largely for compatibility with C syntax. You may find
it more clear to use the first form only.

Actually, they are slightly different.

class A {
public:
A(int);
private:
A(const A&);
};

A ok(1);
A error = 2;

The last line should not compile. It instructs the compiler to create a
temporary A-object from the integer 2 and use the copy constructor to
construct 'error' from this temporary. The compiler is allowed to optimise
this away, but it is not allowed to waive the access priviliges needed.
And as the copyconstructor is private, we cannot construct 'error' from
the temporary A object.

The line that constructs 'ok' is fine.

The lesson is to not use 'T var=value' but 'T var(value)' instead. It
shields you from subteleties like this.

[ For those wondering, the compiler is allowed to optimise the second form
into the first *even* if copy constructing has side effects, so this

class A {
public:
A(int) { puts("A::A(int)"); }
private:
A(const A&) { puts("A::A(const A&)"); }
};

A ok = 1;

Is allowed to print either:

A::A(const A&)
A::A(int)

or only

A::A(int)

]

HTH,
M4
 

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
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top