so basically this is what I am doing:
myClass a,b,c;
a = myClass( ... );
b = myClass( ... );
c = a+b;

this is simple enough, but now I have to overload the operator...

myClass myClass::operator+ (myClass f)
{
     myClass temp;

     temp.value1 = value1*f.value1;
     temp.value2 = value2*f.value2;
     temp.value3 = value3*f.value3;
     return temp;
}

I know I can access the fields directly by dotting into them, but what if I want to refer to the whole datatype, I can easily say temp or f for two of them, but I need to refer to the 3rd one. How do I do that? I know in java I would say "this" what would be the equivalent?

In other words when I run this program:
c=a +b I can easily refer to b as f while i am in the function. How do I refer to a?

Dani AI

Generated

Short answer: inside a non-static member function the left-hand operand is the implicit object; work with it by dereferencing the this pointer (or binding *this to a reference). and were pointing at that. A cleaner, safer approach is to implement mutation once (operator+=) and build operator+ on top of it so you avoid duplicating logic.

A common, idiomatic member implementation (note the const correctness and that the function does not modify the operands) looks like this:

myClass myClass::operator+(const myClass& rhs) const {
    myClass result = *this;   // copy left operand
    result += rhs;            // reuse operator+=
    return result;            // returned by value (RVO/move)
}

If you prefer a non-member for symmetry (as suggested), the modern idiom is to take the left operand by value and the right by const reference; this lets you implement + in terms of += and benefit from move semantics:

myClass operator+(myClass lhs, const myClass& rhs) {
    lhs += rhs;
    return lhs;
}

Notes and cautions:

  • Make operator+ a const member if it does not modify the left operand.
  • Prefer passing the right-hand operand as const myClass& to avoid needless copies in pre-C++11; in C++11+ the pass-by-value-left pattern can be more efficient.
  • Do not return references or pointers to local objects.
  • Static functions and non-member overloads have no this.
  • Keep operator+= as the single place that mutates internals; that makes maintenance and correctness much easier.

These patterns let you "refer to the whole object" (use *this) while following good C++ practice.

Recommended Answers

All 4 Replies

yes c++ has a this pointer which is intrinsically passed

you can use it in any of these following ways
1.this->value
2.(*this).value

yes c++ has a this pointer which is intrinsically passed

you can use it in any of these following ways
1.this->value
2.(*this).value

I don't want it to point to a particular field, I want to refer to the whole object

Ussually when you are writting the + operator it is not really a member function of a class here is how I would implement it

#include <iostream>
#include <ostream>

class someClass
{
    public:
        int getX();
        void setX(const int &toSet);
        someClass &operator=(const someClass &toSet);
        someClass &operator+=(const someClass &toAppend);
        friend someClass operator+(const someClass &a, const someClass &b);
    private:
        int x;
};

int someClass::getX()
{
    return this->x;
}

void someClass::setX(const int &toSet)
{
    this->x = toSet;
}

someClass &someClass::operator=(const someClass &toSet)
{
    this->x = toSet.x;
    return *this;
}

someClass &someClass::operator+=(const someClass &toAppend)
{
    this->x += toAppend.x;
    return *this;
}

someClass operator +(const someClass &a, const someClass &b)
{
    someClass temp = a;
    temp+= b;
    return temp;
}

int main()
{
    someClass a;
    someClass b;
    someClass c;
    
    a.setX(10);
    b = a;
    c = a + b;
    
    std::cout<<"a's x = "<<a.getX()<<std::endl;
    std::cout<<"b's x = "<<b.getX()<<std::endl;
    std::cout<<"c's x = "<<c.getX()<<std::endl;
    
    std::cin.get();
    
    return 0;
}

Since this is a pointer to the object, *this dereferences this pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.