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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:11:31+00:00 2026-06-18T06:11:31+00:00

I’m new to c++ (coming from Java and C#), and I’m trying to override

  • 0

I’m new to c++ (coming from Java and C#), and I’m trying to override the == operator in one of my classes so I can see if I have 2 object that have the same value for a given property. I’ve been doing a bunch of googling and trying to make something that works. What I need is for the == operator to return TRUE when 2 objects have the same _name text.

Here’s the header file:

//CCity.h -- city class interface
#ifndef CCity_H
#define CCity_H

#include <string>

class CCity
{
friend bool operator ==(CCity& a,  CCity& b)
{
    bool rVal = false;
    if (!(a._name.compare(b._name)))
        rVal = true;
    return rVal;
}
private:
    std::string _name;
    double _x; //need high precision for coordinates.
    double _y;
public:
    CCity (std::string, double, double); //Constructor
    ~CCity (); //Destructor
    std::string GetName();
    double GetLongitude();    
    double GetLatitude();
    std::string ToString();
};
#endif

In my main() method:

    CCity *cit1 = new CCity("bob", 1, 1);
    CCity *cit2 = new CCity("bob", 2, 2);
    cout<< "Comparing 2 cities:\n";
    if (&cit1 == &cit2)
        cout<< "They are the same \n";
    else
        cout << "They are different \n";
    delete cit1;
    delete cit2;

The problem is that my code in the friend bool operator == block never gets executed. I feel like I’m doing something wrong with either how I’m declaring that operator, or how I’m using it.

  • 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-18T06:11:32+00:00Added an answer on June 18, 2026 at 6:11 am

    & takes the address of (you’re comparing pointers), when really you want to dereference using *:

    if (*cit1 == *cit2)
        cout<< "They are the same \n";
    

    Anyway, there is absolutely no point to using pointers here, let alone dumb ones.

    Here’s how it would look without them (the proper way):

    CCity cit1("bob", 1, 1);
    CCity cit2("bob", 2, 2);
    cout<< "Comparing 2 cities:\n";
    if (cit1 == cit2)
        cout<< "They are the same \n";
    else
        cout << "They are different \n";
    

    Also, as WhozCraig mentions, consider using const-ref parameters for your operator== function since it shouldn’t modify the arguments.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.