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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:53:43+00:00 2026-05-22T17:53:43+00:00

I am trying to understand operators you need to overload when working with custom

  • 0

I am trying to understand operators you need to overload when working with custom classes in STL(SCL).

Can any one please tell me what is it I am doing wrong ?

class myClass
{
public:
    int data;
    myClass()
    {
        data =0;
        cout<<"Default const "<<endl;
    }

    myClass(int x)
    {
        data = x;
        cout<<"Int constructor"<<endl;
    }

    myClass(const myClass &m)
    {
        cout<<"Copy constructor"<<endl;
    }

    bool operator == (const myClass &temp)
    {
        cout<<"Operator called &";
        return data == temp.data;
    }

    bool operator == (const myClass *temp)
    {
        cout<<"Operator called *";
        return data == temp->data;
    }
};

int main ()
{
    /*
    vector<int> myvector;
    myvector.push_back(10);
    myvector.push_back(20);
    myvector.push_back(30);

    cout << "myvector contains:";
    for_each (myvector.begin(), myvector.end(), meObj);
    */

    vector<myClass*> myVec;
    myClass temp;
    myVec.push_back(&temp);
    myClass temp2(19);
    myVec.push_back(&temp2);
    myClass temp3(19);

    vector<myClass*>::iterator it = find(myVec.begin(),myVec.end(),&temp2); //works
    if(it!=myVec.end())
    {
        cout<<"Value is "<<(*it)->data;
    }

    vector<myClass*>::iterator dit = find(myVec.begin(),myVec.end(),&temp3); //fails
    if(dit!=myVec.end())
    {
        cout<<"Value is "<<(*dit)->data;
    } 

    cout << endl;

    return 0;
}

Please correct me if I am wrong, but the first find works as it does a address comparison. What do I need to overload for the above to work ?

Do both the signature make sense ?

bool operator == (const myClass &temp); // seen in many places
bool operator == (const myClass *temp); // what if two pointer types of same object are being compared?

Cheers!

  • 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-22T17:53:44+00:00Added an answer on May 22, 2026 at 5:53 pm

    Operator overloads must have at least one user-defined type. So you cannot overload operator== for two pointers, for instance.

    Your myClass::operator==(const myClass *temp) is valid in the sense that it compiles, but makes very little semantic sense, and is not recommended (there are very few situations where you’d want to do T x; T *y; ... (x == y)).

    For your situation, where you have a vector of pointers, you may want to consider std::find_if, which takes a predicate. Something like:

    class CompareByPointer
    {
    public:
        explicit CompareByPointer(const myClass &p) : p(p) {}
        bool operator() (const myClass &rhs) const { return p->data == rhs->data; }
    private:
        const myClass &p;
    };
    
    ...
    
    find_if(myVec.begin(), myVec.end(), CompareByPointer(&temp2));
    

    [As a side note, you should generally define member functions const wherever possible. So your operator overloads should be const.]

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

Sidebar

Related Questions

Sorry if this doesn't make any sence, but I'm trying my best to understand
Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
After trying to understand why client code is not rendered in a page (injected
I trying to understand if a isset is required during form processing when i
Just trying to understand that - I have never used it before. How is
I've been trying to understand how Ruby blocks work, and to do that I've
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is

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.