Self Assignment check in Copy ctor

A

AlgoMan

Do you need to provide a self assignment check in a copy ctor
(like you provide in an assignment operator).

If not, why not?
 
A

Artie Gold

AlgoMan said:
Do you need to provide a self assignment check in a copy ctor
(like you provide in an assignment operator).

If not, why not?
<sarcasm>
"Do you smell something?"

"Yeah."

"What is it?"

"Wait...it's, it's..."

"Yup. Smells like homework to me..."

"Me too."
</sarcasm>

Two words: What `self'?

HTH,
--ag
 
C

Clark S. Cox III

Do you need to provide a self assignment check in a copy ctor
(like you provide in an assignment operator).

If not, why not?

Just think about it, what does a self assignment check actually check for?
 
R

Risto Lankinen

Artie Gold said:
Two words: What `self'?

Well, you know, the... the... the 'self':

- - -

struct C
{
C( const C &r )
{
if( this == &r )
{
cout << "Hello myself!" << endl;
}
}
};

int main()
{
C c(c);
}

- - -

(Go ahead, try it out if you don't believe it works!)

Because the risk is very very small that somebody would
accidentally copy-initialize with reference to self. It
can be done, but the only way to do it is deliberately.

- Risto -
 
R

Ron Natalie

AlgoMan said:
Do you need to provide a self assignment check in a copy ctor
(like you provide in an assignment operator).

If not, why not?
It's possible to initialize an object with itself, but it's not overly
useful nor something most worry about. Assignment is another matter.
 

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