473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Self Assignment check in Copy ctor

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

If not, why not?

Jul 23 '05 #1
4 2080
AlgoMan wrote:
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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
On 2005-02-05 16:45:27 -0500, "AlgoMan" <al*****@mailinator.com> 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?


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

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #3
> AlgoMan wrote:
Do you need to provide a self assignment check in a copy ctor
(like you provide in an assignment operator).

Artie Gold <ar*******@austin.rr.com> wrote in message news:<36*************@individual.net>... 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!)
AlgoMan wrote:
If not, why not?


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 -
Jul 23 '05 #4
AlgoMan wrote:
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.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
8
by: slurper | last post by:
if i have a vector and assign another vector to a vector variable like this: vector<int> c, k; c.push_back(1), c.push_back(2); k.push_back(3), k.push_back(4); c = k; does this work?? if...
10
by: ambar.shome | last post by:
Dear All, Whats the difference between a copy constructor and assignment operator. We can assign the values of member variables of one object to another object of same type using both of them....
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
1
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.