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

  • Home
  • SEARCH
  • 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 8144543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:25:00+00:00 2026-06-06T13:25:00+00:00

I have the following code: class Employee { friend string FindAddr( list<Employee> lst,string name

  • 0

I have the following code:

class Employee {
friend string FindAddr( list<Employee> lst,string name );
public:
Employee(const string& s){ cout << "Employee CTOR" << endl;}
bool operator==( Employee& e) {
    return e.name == name;
}
private:
string name;
string addr;
};


string FindAddr( list<Employee> lst, string name ) { 
string result = "";
for( list<Employee>::iterator itr = lst.begin(); itr != lst.end(); itr++ ) { 
    if ( *itr == name ) { // Problematic code
        return (*itr).addr;
    }
}
return result;
}

As I understand, the problematic line if ( *itr == name ) should follow these steps:

  1. Recognizing it is operator== on class Employee.
  2. Trying to figure out if there is a conversion from string name to Employee so that the operator could work.
  3. Implicitly call the constructor Employee(const string& s) on object string name.
  4. Continue with operator==.

However, this line gives me trouble at compile time:

Invalid operands to binary expression ('Employee' and 'string' (aka 'basic_string<char>'))

Even if I explicitly call the constructor:

if ( *itr == Employee::Employee(name) )

I get the same error.

This is confusing. I’m having trouble to understand when implicit constructor call works (and why the code doesn’t work even if I explicitly call the constructor).

Thanks!

  • 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-06T13:25:02+00:00Added an answer on June 6, 2026 at 1:25 pm

    The rule is:
    Temporaries can be bound only to a const reference.

    As you mentioned for the == to work,
    The object name which is of the type std::string needs to be converted to type Employee, the conversion operators in your class Employee should make this happen. However, the created Employee object is an temporary object.i.e a nameless object which doesn’t live long enough to need a name.Such a nameless temporary object cannot be bound to a non-const reference.[Ref 1]
    So what you need is a const reference:

    bool operator==(const Employee& e)
                    ^^^^^^
    

    [Ref 1]
    Motivation for the rule:
    Motivation for this particular rule is outline by Bajrne in Section 3.7 of The design and evolution of C++.

    I made one serious mistake, though, by allowing a non-constant reference to be initialized by an an non l-value. For example:

    void incr(int &rr) {r++;}    
    
    void g()
    {
        double ss = 1;
        incr(ss);    //note: double passed int expected
    }
    

    Because of the difference in the type the int& cannot refer to the double passed so a temporary was generated to hold an int initialized by ss‘s value. Thus the incr() modified the temporary, and the result wasn’t reflected back to the function.

    Thus many a times temporary objects are generated unknowingly in function calls, where they are least expected and one might (wrongly)assume that their function works on the original object being passed, whereas the function operates on the temporary.Thus it’s easy to write a code that assumes one thing and does another.So to avoid such easy to mislead situations the rule was put in place.

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

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have following code public class TEST { public static void main(String arg[]){ try
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following code: class Program { static void Main(string[] args) { new
Say I have the following code: class Parent { static string MyField = ParentField;
Let's say I have the following entity: public class Store { public List<Product> Products
I have the following code : TypedQuery<Employee> query = em.createNamedQuery(QueryName, Employee.class); which result the
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following code: class outer { struct inner { int var1; int
I have the following code: class Foo<T> where T : struct { private T

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.