StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Tuesday, June 17, 2008

C++ Copy Assignment Operator

 June 17, 2008     CPP     No comments   

What is copy assignment operator in C++?

  • The copy assignment operator allows to assign objects of same class to each other by overloading '=' operator.
  • The compiler automatically generates this member function if is not defined explicitly.
  • The generated copy assignment funcion does a bit wise copy or in other words shallow copy.
  • If our class has dynamically allocated members then it becomes necessary to write our own implementation of copy assignment function to perform a deep copy.

Demonstrate the usage of copy assignment operator

#include <iostream>
using namespace std;

class MyClass {  
    private:      
        char* str;  
    public:      
        MyClass();
        MyClass(char* aStr);      
        MyClass& operator=(const MyClass& obj);      
        void Print();      
        ~MyClass();
};

MyClass::MyClass() {
}

MyClass::MyClass(char* aStr) {  
    cout << "In constructor ..." << endl;  
    str = strdup(aStr);
}

// Copy assignment
MyClass& MyClass::operator=(const MyClass& other) {
    cout << "In copy assignment ..." << endl;
    str = strdup(other.str);
    return *this;
}

void MyClass::Print() {  
    cout << str << endl;
}

MyClass::~MyClass() {  
    cout << "In destructor ..." << endl;  
    delete str;
}

void main()
{  
    // Create obj1
    MyClass* obj1 = new MyClass("Hello World");
    obj1->Print();

    // Assignment. obj1 to obj2
    MyClass obj2;
    obj2 = *obj1;

    // Cleanup obj1
    delete obj1;

    obj2.Print();
}
OUTPUT:-
In constructor ...
Hello World
In copy assignment ...
In destructor ...
Hello World
In destructor ...
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • C++ new and deleteHow is dynamic memory management handled in C++? C++ supports the operators new and delete for dynamic memory management. These operators perform … Read More
  • C++ Copy Assignment OperatorWhat is copy assignment operator in C++? The copy assignment operator allows to assign objects of same class to each other by overloading '=' opera… Read More
  • C++ this pointerWhat is this pointer? Every object has a special pointer "this" which points to the object itself. This pointer is accessible to all members of th… Read More
  • CDT Plugin for EclipseUsing CDT Plugin for Eclipse I have recently started using C++ development using eclipse and found it interesting and thought of sharing the setup de… Read More
  • C++ Placement NewWhat is placement new in C++? In some scenarios it becomes necessary to place objects at specified locations in memory. Placement new solves this p… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © 2025 StackStalk