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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:49:59+00:00 2026-06-11T02:49:59+00:00

Normally you can compare strings.. if (var1 == a string) … But when I

  • 0

Normally you can compare strings..

if (var1 == "a string") ...

But when I declare my own class with the conversion operator, like this :

class my_type {
   operator std::string() const { ... };
   ....
}

Now this :

 std::string var1("a string");
 my_type x("a string");
 if (x == "a string") ....
 if (x == var1) ....

does not work.. i.e….

error: no match for ‘operator==’ 

Of course this works :

 if ((std::string) x == var1) ....

But I want it to happen w/o explicitly casting.
why does not c++ convert my_type to string for comparison..
How can I force it to do this w/o implementing the “==” operator itself ?
The same goes for the other comparison operators.

thank you

PS> Btw if I implement operator which convert my_type to numbers(it is OK for my type)…like :

 operator double() const { ... };

the comparison with numbers work ok, I don’t need to implement ==,etc…..

  • 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-11T02:50:00+00:00Added an answer on June 11, 2026 at 2:50 am

    As pointed out here, this isn’t about implicit casts, but of how operator == behaves on strings.

    I however suggest you overload operator == for your class and not rely on an implicit conversion.

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

Sidebar

Related Questions

Normally you can print a string in C like this.. printf(No record with name
Normally I'm pretty competent with CSS but I just can't figure this one out...
Normally I can call this.GetType(), but I can't access this in a static method.
RUBY_PLATFORM is java either way. Normally I can do system checks with uname but
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
Normally to use Ruby libraries from command line I can do something like: ruby
Normally Eclipse 'File Compare' compares files in vertical panels like: Is there a way
Normally you can execute a Python script for example: python myscript.py , but if
Normally i can listen to boot complete action.but when user explicitly force stop my
In my html.erb views I can normally use code like: if current_user.role?(:label) do something

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.