简体   繁体   中英

reason for not using overloaded assignment and copy constructor in Static Array

I am pretty new to C++ and this website so I don't know if my question will be turned down but I have a question that I need to know. Its not really a "program" related question per-say.

so my question is: what is the reason that Overloaded assignment operator and copy constructor is not used in class that uses regular array (its called a Static array(?) I believe) My reason is that,since memory management is not required in static array, they get destroyed when the function ends (or when it it returned)thus making overloaded assignment operator and copy constructor not possible.

Am I correct or am I way off? And yes, I did try asking this question before without providing any answers but this is what I could come up with. (based on why destructor is not used)

The defaulted copy constructor and copy assignment operator simply copy over the members from one object to the new object.

Consider a class that has a member int* arr; that points at a dynamically allocate array that was allocated in the class's constructor. When you use the defaulted copy constructor or copy assignment operator for this class, it will just copy the value of the int* across. The dynamically allocated array is not a member of the object so isn't copied. That means the copy and the original will both be pointing at the same dynamically allocated array. Now, if the class's destructor has delete arr; in it (which it should), when each of the objects is destroyed, they will both attempt to destroy the same dynamically allocated array. That's bad news. Once one of them has performed the deallocated, the other can't. To get around this, you need to provide a copy constructor, assignment operator, and destructor to handle the dynamically allocated memory (see the rule of three ).

In contrast, if your class has a member int arr[10]; , for example, the array is part of the objects of that class type. When you use the defaulted copy constructor or copy assignment operator, the array itself is copied across. This means that the elements in the new object are copies of the elements in the other class. This is perfectly fine and leads to no problems. In fact, you can even use the defaulted destructor because you have nothing to delete .

As noted, it's not clear what you are trying to find out, but I will point out a few things based on the language you're using. First, memory management is always required--just sometimes, you don't have to do it by hand. I'm not sure what you mean by "not used", but the point of "static" data in a class is that all instances of that class refer to the same data--there's nothing to copy, because each class is referring to the same thing. Overloading the operator and constructor are still possible, just not relevant for static data. Likewise, static data in a method is not a local variable; it persists from call to call in that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM