Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:20:57+00:00 2026-05-18T23:20:57+00:00

I made the following operator overloading test: #include <iostream> #include <string> using namespace std;

  • 0

I made the following operator overloading test:

#include <iostream>
#include <string>

using namespace std;

class TestClass
{
    string ClassName;

    public:

    TestClass(string Name)
    {
        ClassName = Name;
        cout << ClassName << " constructed." << endl;
    }

    ~TestClass()
    {
        cout << ClassName << " destructed." << endl;
    }

    void operator=(TestClass Other)
    {
        cout << ClassName << " in operator=" << endl;
        cout << "The address of the other class is " << &Other << "." << endl;
    }
};

int main()
{
    TestClass FirstInstance("FirstInstance");
    TestClass SecondInstance("SecondInstance");

    FirstInstance = SecondInstance;
    SecondInstance = FirstInstance;

    return 0;
}

The assignment operator behaves as-expected, outputting the address of the other instance.

Now, how would I actually assign something from the other instance? For example, something like this:

void operator=(TestClass Other)
{
    ClassName = Other.ClassName;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:20:58+00:00Added an answer on May 18, 2026 at 11:20 pm

    The code you’ve shown would do it. No one would consider it to be a particularly good implementation, though.

    This conforms to what is expected of an assignment operator:

    TestClass& operator=(TestClass other)
    {
        using std::swap;
        swap(ClassName, other.ClassName);
        // repeat for other member variables;
        return *this;
    }
    

    BTW, you talk about “other class”, but you have only one class, and multiple instances of that class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made the following program #include <iostream> #include <typeinfo> template<class T> struct Class {
I've made the following extension method ... public static class ObjectExtensions { public static
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
Given the following template: template <typename T> class wrapper : public T {}; What
I'm working with a class for which the new operator has been made private,
I am currently doing a code review and the following code made me jump.
I have an application that is made up of the following: A central database
I have roughly the following code. Could this be made nicer or more efficient?
While discussing a Java synchronization question , someone made a comment that the following
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.