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 8192961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:18:56+00:00 2026-06-07T04:18:56+00:00

In the following codes, I illustrate an example of operator overloading: #include <iostream> using

  • 0

In the following codes, I illustrate an example of operator overloading:

#include <iostream>
using namespace std;

template <typename T>
class A
{
public:
    A()   {};
    A( T &obj) {value = obj;};
    ~A() {};
    T value;
    template <typename E>
    A<T>& operator = (const A<E> &obj)
    {
        cout<<"equal operator"<<endl;
        if(this == &obj)
            return *this;

        value = obj.value;
        return *this;
    }

};



int main()
{
    int temp;
    temp = 3;
    A<int> myobjects(temp);
    cout<<myobjects.value<<endl;

    temp = 7;
    A<int> yourobjects(temp);
    yourobjects = myobjects;
    cout<<yourobjects.value<<endl;



    return 0;
}

However, when I debug this program, I find that the main program does not invoke the equal operator overloading function. However, if I change the equal operator in the following way:

   A<T>& operator = (const A<T> &obj)
    {
        cout<<"equal operator"<<endl;
        if(this == &obj)
            return *this;

        value = obj.value;
        return *this;
    }

It will work. Do you have any ideas why the initial function does not work?

  • 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-06-07T04:18:58+00:00Added an answer on June 7, 2026 at 4:18 am

    Your template version of assignment operator does not suppress the generation of compiler-provided non-template copy-assignment operator for your class. The compiler will implictly declare and define a copy-assignment operator with the following signature

    A<T>& operator =(const A<T>&);
    

    In the overload resolution process for copy-assignment the compiler-provided version wins (since it is more specialized).

    Your template version of assignment operator will only be considered for conversion-assignment. E.g. if at some point you’ll want to assign A<int> object to A<double> object, your template version of assignment operator will be used. But when you assign A<int> to A<int> your operator is ignored since the compiler-declared version is a better match.

    When you declare your own version of copy-assignment with

    A<T>& operator =(const A<T>&);
    

    signature, it suppresses the compiler-generated one. Your version gets used.

    This means that if you want to have your own copy-assignment operator as well as template conversion-assignment operator, you need to explicitly implement both in your class.

    P.S. As ‘@Cheers and hth. – Alf’ correctly noted, your template version of assignment operator is not even valid in general case. Pointers this and &obj generally have different unrelated types. You are not allowed to compare pointers of different unrelated types.

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

Sidebar

Related Questions

I have the following test code #include <iostream> template <typename T> struct PS {
I'll illustrate the doubt in my mind by using the following example I came
I have the following codes <div class=row-fluid> <div class=span6><img src = <?php echo $photo
I am using the following codes, to replace a JLabel each 0.5 seconds, with
In the following example code I illustrate how I solved passing the request from
I'll illustrate with an example: I need to convert the following html with javascript
I'll illustrate with an example: I need to convert the following html with javascript
I have used the following codes: bar([c f], 0.1,'stacked'); and bar( [c , f
I am trying the following codes to suffle the elements of an ArrayList -
I have this following codes: @implementation MyImageView @synthesize image; //image is a UIImage -

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.