Open In App

How to Provide a Swap Function for My Class in C++?

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In C++, providing a swap function for the class allows efficient swapping of the objects of that class type which can be important for many algorithms like sorting algorithms or container manipulations. In this article, we will learn how to implement a swap function for our own user defined class.

swap Function for User Defined Class in C++

There are two ways in which we can provide swap function for our class.

  • By defining the whole swap logic by ourselves.
  • By using the inbuild swap function where we can.

The second method is better as it provides a concise and efficient method of swapping in most of the cases.

Implementing the swap Function using std::swap

To provide the swap function for your class using std::swap, follow the below steps:

  1. Define the swap function inside your class.
  2. Provide a non-member swap function that calls the member swap function.
  3. Optionally, provide a specialized std::swap function template for your class.

Need of Custom Swap Function?

By default, the STL uses std::swap to exchange the values of two objects. While std::swap works well for many cases, it may not be the most efficient for your custom class. Implementing a custom swap function allows you to control how member variables are exchanged, potentially improving performance by avoiding unnecessary copying or allocation operations.

C++ Program to Implement Swap Function

Here’s how you can implement a swap function for the custom class:

C++
// C++ program to implement swap function

#include <iostream>
using namespace std;

class MyClass
{
  public:
    MyClass(int a, double b) : data1(a), data2(b)
    {
    }

    // Member swap function
    void swap(MyClass &other) noexcept
    {
        // Swap data1 members
        std::swap(data1, other.data1);
        // Swap data2 members
        std::swap(data2, other.data2);
    }

    // Display function for demonstration purposes
    void display() const
    {
        cout << "data1: " << data1 << ", data2: " << data2 << endl;
    }

  private:
    int data1;
    double data2;
};

// Non-member swap function for better accessibility
void swap(MyClass &first, MyClass &second) noexcept
{
    first.swap(second);
}

int main()
{
    MyClass obj1(1, 1.1), obj2(2, 2.2);

    cout << "Before swap:" << endl;
    // Display obj1 before swap
    obj1.display();
    // Display obj2 before swap
    obj2.display();

    // Swap the objects
    swap(obj1, obj2);

    cout << "After swap:" << endl;
    // Display obj1 after swap
    obj1.display();
    // Display obj2 after swap
    obj2.display();

    return 0;
}

Output
Before swap:
data1: 1, data2: 1.1
data1: 2, data2: 2.2
After swap:
data1: 2, data2: 2.2
data1: 1, data2: 1.1

Benefits of Providing a swap Function

  • Performance: The Swapping objects using swap is often more efficient than copying objects.
  • Standard Library Compatibility: Many standard library algorithms rely on the efficient swapping such as the sorting and containers like std::vector and std::map.
  • Exception Safety: The swap can facilitate exception-safe code by the enabling the copy-and-swap idiom.

Article Tags :
Practice Tags :

Similar Reads