0% found this document useful (0 votes)
23 views4 pages

Assignment and Copy Initialization in C

Good for assignment and copy operator in cpp

Uploaded by

parshantgoyal69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Assignment and Copy Initialization in C

Good for assignment and copy operator in cpp

Uploaded by

parshantgoyal69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment and Copy Initialization in C++

1. Assignment in C++

 Assignment is the process of copying the contents of one object into


another existing object of the same type.
 In C++, this is usually done using the assignment operator (`=`). By
default, C++ provides a built-in assignment operator for each class, which
performs shallow copying (member-wise copying).

a. Default Assignment Operator:


 By default, C++ generates an assignment operator for each class that
performs a member-wise assignment. For simple data types like `int` or
`double`, this is often enough.
 However, for classes that allocate dynamic memory (e.g., via pointers),
it's usually necessary to overload the assignment operator.
b. Overloading the Assignment Operator:
 When you need deep copying (especially when dynamic memory is
involved), you need to overload the assignment operator.
 This allows you to properly manage resources, avoid memory leaks, and
handle issues like self-assignment.
Syntax for Overloading the Assignment Operator:
class MyClass { }
int* data; // Pointer to dynamically allocated memory
// Display the value
public: void display() const {
// Constructor std::cout << "Value: " << *data << std::endl;
MyClass(int val) { }
data = new int(val); };
}
// Destructor int main() {
~MyClass() { MyClass obj1(10);
delete data; MyClass obj2(20);
}
// Copy Assignment Operator obj1.display(); // Output: Value: 10
MyClass& operator=(const MyClass& other) { obj2.display(); // Output: Value: 20
if (this != &other) { // Check for self-assignment
// Release the existing memory obj2 = obj1; // Assign obj1 to obj2
delete data;
// Allocate new memory and copy obj1.display(); // Output: Value: 10
data = new int(*(other.data)); obj2.display(); // Output: Value: 10
} }
return *this;
2. Copy Initialization in C++

Copy Initialization is the process of creating a new object by copying an


existing object. This usually happens when:

1. You create a new object and initialize it with another object.

2. An object is passed by value to a function.

3. An object is returned by value from a function.

Copy initialization uses the copy constructor (or the move constructor, if
applicable). If a class doesn't define a copy constructor, C++ provides a default
one that performs shallow copying (member-wise copying).

a. Default Copy Constructor:

Similar to the assignment operator, C++ automatically generates a copy


constructor for every class that copies each data member from the source object
to the new object.

b. Custom Copy Constructor (Deep Copy):

If a class manages resources such as dynamic memory, you'll need to implement


a custom copy constructor to handle deep copying.

Syntax for a Copy Constructor:


class MyClass {
int* data; // Pointer to dynamically allocated memory // Display the value
void display() const {
public: std::cout << "Value: " << *data << std::endl;
// Constructor }
MyClass(int val) { };
data = new int(val);
} int main() {
MyClass obj1(30); // Constructor called
// Destructor MyClass obj2 = obj1; // Copy constructor called
~MyClass() {
delete data; obj1.display(); // Output: Value: 30
} obj2.display(); // Output: Value: 30
}
// Copy Constructor
MyClass(const MyClass& other) {
// Allocate new memory and copy the data
data = new int(*(other.data));
}
Key Points:

1. Deep Copying: The copy constructor creates a new memory block and copies
the data from the source object.

2. Copy Constructor Invocation: The copy constructor is called when a new


object is created from an existing one, e.g., `MyClass obj2 = obj1;`.

c. Copy Constructor vs. Assignment Operator:

- Copy Constructor: Called when a new object is created and initialized using
another object (e.g., `MyClass obj2 = obj1;`).

- Assignment Operator: Called when an existing object is assigned the value of


another existing object (e.g., `obj2 = obj1;`).

Example of Copy Initialization:

class MyClass { // Display


int* data; void display() {
std::cout << "Data: " << *data << std::endl;
public: }
// Constructor };
MyClass(int val) {
data = new int(val); int main() {
} MyClass obj1(10); // Constructor called
MyClass obj2 = obj1; // Copy constructor
// Copy Constructor called (Copy Initialization)
MyClass(const MyClass& obj) {
data = new int(*(obj.data)); obj1.display(); // Output: Data: 10
} obj2.display(); // Output: Data: 10
}
// Destructor
~MyClass() {
delete data;
}

Differences between Assignment and Copy Initialization:


Practical Considerations:

- Use deep copying (via overloading the assignment operator or defining a


custom copy constructor) when your class uses dynamically allocated memory
or other resources.

- Always remember to check for self-assignment in the assignment operator to


avoid issues.

- The copy constructor is vital for passing and returning objects by value, so
ensure proper handling of resources.

You might also like