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?

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.