Page 1 of 1

## 4 Replies - 438 Views - Last Post: 12 November 2012 - 09:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299486&amp;s=0dc604fa3921756ceea8092b2423ca84&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 koricha

Reputation: 0
• Posts: 85
• Joined: 08-February 12

Posted 09 November 2012 - 03:56 PM

This code works fine, i have trouble understanding how does this 2 following function work.

```bool Vector2::operator==(const Vector2& v) const{
return ((x_ == v.GetX()) && (y_ == v.GetY()));
}

bool Vector2::operator!=(const Vector2& v) const{
return (!((*this) == v));
}
```

i don't understand how x_ and v.GetX() are having 2 different outputs. in other words do they point to the same variable

```using std::cout;
using std::cin;
using std::ostream;
using std::istream;

class Vector2{
private:
float x_;
float y_;
public:
Vector2();

Vector2(float x, float y);
~Vector2();
float GetX() const;
float GetY() const;

void SetX(const float x);
void SetY(const float y);

bool operator==(const Vector2& v) const;
bool operator!=(const Vector2& v) const;

ostream& toStream(ostream& os) const;
istream& fromStream(istream& is);
};

ostream& operator<<(ostream& os, const Vector2& v);
istream& operator>>(istream& is, Vector2& v);

#endif VECTOR2_H
```

```#include "Vector2.h"

Vector2::Vector2() : x_(0),y_(0)
{}

Vector2::Vector2(float x, float y)
: x_(x),y_(y)
{}

Vector2::~Vector2()
{}

float Vector2::GetX() const{
return x_;
}

float Vector2::GetY() const{
return y_;
}

void Vector2::SetX(const float x){
x_ = x;
}

void Vector2::SetY(const float y){
y_ = y;
}

bool Vector2::operator==(const Vector2& v) const{
return ((x_ == v.GetX()) && (y_ == v.GetY()));
}

bool Vector2::operator!=(const Vector2& v) const{
return (!((*this) == v));
}

ostream& Vector2::toStream(ostream& os) const{
os << "Vec x: " << x_ <<"\n";
os << "Vec y: " << y_ <<"\n";

return os;
}

istream& Vector2::fromStream(istream& is){
cout<< "Enter X: ";
is>> x_;
cout<< "Enter Y: ";
is>> y_;

return is;
}

ostream& operator<<(ostream& os, const Vector2& v){
return (v.toStream(os));
}

istream& operator>>(istream& is, Vector2& v){
return (v.fromStream(is));
}

```

```#include <iostream>
#include "Vector2.h"

using namespace std;

int main(){

cout.precision(5);
cout.setf(ios::showpoint);

Vector2 vec1;
Vector2 vec2;

cin>>vec1>>vec2;
cout<<endl;

if(vec1==vec2){
cout<<"vec1 is equal to vec2: \n";
cout<<vec1<<vec2<<endl;
}else{
cout<<"vec1 is not equal to vec2: \n";
cout<<vec1<<vec2<<endl;
}

cout<<"Enter new values for vec1 and vec2: \n"<<endl;
cin>>vec1>>vec2;

if(vec1!=vec2){
cout<<"vec1 is not equal to vec2: \n";
cout<<vec1<<vec2<<endl;
}else{
cout<<"vec1 is equal to vec2: \n";
cout<<vec1<<vec2<<endl;
}

return 0;
}
```

Is This A Good Question/Topic? 0

### #2 Xupicor

• Nasal Demon

Reputation: 334
• Posts: 869
• Joined: 31-May 11

Posted 09 November 2012 - 04:11 PM

x_ is the same as this->x_ (unless shadowing occurs, that is), and v.getX() calls a member function on (possibly) different object. What do you not understand?

If you do this:
```vector == vector;// calls: vector.operator==(vector)
```

then they will return value of the same variable (because this and &v inside the overloaded operator member function body will point to the same object.

Since x_ is not static member, every instantiation (object) of that class will keep its own, separate variable x_.

This post has been edited by Xupicor: 09 November 2012 - 04:13 PM

### #3 mojo666

Reputation: 382
• Posts: 827
• Joined: 27-June 09

Posted 09 November 2012 - 04:12 PM

suppose instead it was written Vector2::isEqual(Vector2 v);

if we use the line vec1==vec2, it is similar to saying vec1.isEqual(vec2)

So, x_ is the x value of vec1, while v.Getx() is vec2.Getx() ...which returns vec2's x value.

### #4 koricha

Reputation: 0
• Posts: 85
• Joined: 08-February 12

Posted 09 November 2012 - 04:18 PM

Xupicor, on 09 November 2012 - 04:11 PM, said:

x_ is the same as this->x_ (unless shadowing occurs, that is), and v.getX() calls a member function on (possibly) different object. What do you not understand?

If you do this:
```vector == vector;// calls: vector.operator==(vector)
```

then they will return value of the same variable (because this and &v inside the overloaded operator member function body will point to the same object.

Since x_ is not static member, every instantiation (object) of that class will keep its own, separate variable x_.

Thank you for the explanation @Xupicor.

mojo666, on 09 November 2012 - 04:12 PM, said:

suppose instead it was written Vector2::isEqual(Vector2 v);

if we use the line vec1==vec2, it is similar to saying vec1.isEqual(vec2)

So, x_ is the x value of vec1, while v.Getx() is vec2.Getx() ...which returns vec2's x value.

thank you, but how do you know that x_ is the x value of vec1, while v.Getx() is vec2.Getx() could it be the opposite side

This post has been edited by koricha: 09 November 2012 - 04:19 PM

### #5 mojo666

Reputation: 382
• Posts: 827
• Joined: 27-June 09

Posted 12 November 2012 - 09:50 AM

Quote

thank you, but how do you know that x_ is the x value of vec1, while v.Getx() is vec2.Getx() could it be the opposite side

The left side is always treated as "this" so the private members can be accessed directly. The right side is the parameter so you have to access members through public methods. This is just the convention of c++. It would only be the opposite if instead of vec1==vec2, it were vec2==vec1 for the reasons stated above.