Christian
Christian

Reputation: 447

Which operator gets invoked upon object evaluation?

I would like to print the id of my object when the object is being evaluated (third line in my main function "one = two; should output "Object id: 2"). I know that the assignment operator is only invoked for the lvalue, a conversation operator to itself will never be called and I don't want to use the function call operator.

Any ideas how this can be done, which operator must I overload?

P.S.: Please ignore any saneness of the code, I only care about identifying the right to-be-overloaded operator.

#include <iostream>    
class Object
{
public:
  Object( int id ) : id_( id )
  {
  }     
  //Assignment operator only invoked for lvalue
  Object& operator= (const Object& other)
  {
    std::cout << "Object id: " << id_ << std::endl;
    return *this;
  }    
  //Conversion operator to itself will never be called
  operator Object() const
  {
    return *(this);
  }    
  //Function call operator - not what I mean
  Object operator()()
  {
    std::cout << "Object id: " << id_ << std::endl;
    return *(this);
  }    
private:
  int id_;
};

int main()
{
  Object one(1);
  Object two(2);
  one = two;
  one = two();
  return 0;
}

Upvotes: 1

Views: 81

Answers (3)

Sneftel
Sneftel

Reputation: 41474

The concept of "object evaluation" isn't really present in C++. An expression can be evaluated, of course, and operator overloading fits into this: foo = bar invokes any overloaded operator= which matches the operands (and in the case of operator= specifically, must be a member of the LHS class type). But it's the assignment that's being evaluated, not foo or bar (and note that in the context of the operator overload, these are present as pointers or references, so it's not inevitable that either one will be evaluated at all).

The appropriate solution here will depend on what it is you're trying to accomplish. But there's no one function that automatically fires whenever your code mentions bar.

Upvotes: 1

Bill Lynch
Bill Lynch

Reputation: 81926

So, this line:

one = two;

Will call this function that you already have:

Object& operator= (const Object& other)
{
    std::cout << "Object id: " << id_ << std::endl;
    return *this;
}

However, let's note that there are two Object objects that are visible in that function.

Object& operator= (const Object& other)
{
    std::cout << "Object(" << this->id_ << ")";
    std::cout << " = ";
    std::cout << "Object(" << other.id_ << ")";
    std::cout << "\n";
    return *this;
}

You can see a full example where we run this code at ideone.com.

Upvotes: 0

Patrick Roberts
Patrick Roberts

Reputation: 51866

#include <iostream>    
class Object
{
public:
  Object( int id ) : id_( id )
  {
  }     
  //Assignment operator only invoked for lvalue
  Object& operator= (const Object& other)
  {
    std::cout << "Object id: " << other.id_ << std::endl;
    return *this;
  }    
private:
  int id_;
};

int main()
{
  Object one(1);
  Object two(2);
  one = two;
  return 0;
}

I tried this to see if Mat was right, turns out it compiles and prints "Object id: 2" as expected.

Upvotes: 0

Related Questions