****Help in operator overloading in classes! *****

SOLVED
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int a,int b) : x(a), y(b) {}
    CVector operator + (const CVector&);
};

CVector CVector::operator+ (const CVector& param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return temp;
}

int main () {
  CVector foo (3,1);
  CVector bar (1,2);
  CVector result;
  result = foo + bar;
  cout << result.x << ',' << result.y << '\n';
  return 0;
}


in this example in the function operator + the CVector temp is not initialized!
how this code works ? also why you need to write this fucntion as a member of class? the left side gets copied to temp? what will happen if you overload +operator? now the right side will get copied to the CVector temp inside the funnction?? why you declare the operator + func outside the class? can you overload operator without defining his classes (for his arguments)
HELP!
can you also give me another example of operator overloading?
What is *(this) when should i use it and return it from function??
Thanks for the help!


answer will get gift
Last edited on
Leo654321 wrote:
in this example in the function operator + the CVector temp is not initialized!
Right, but then you assign to both values, initializing them.
Leo654321 wrote:
also why you need to write this fucntion as a member of class?
This particular operator doesn't have to be written as a member of the class (and in fact it should not be written as a member of the class).
Leo654321 wrote:
the left side gets copied to temp?
No, the sum of the left and right operands is assigned to temp.
Leo654321 wrote:
what will happen if you overload +operator?
The code will not compile, the operator you are overloading must always appear to the right of the key word operator.
Leo654321 wrote:
now the right side will get copied to the CVector temp inside the funnction??
No, the sum of the left and right operands is assigned to temp.
Leo654321 wrote:
why you declare the operator + func outside the class?
The only reason I can think is to show the syntax, since it is better to define this particular operator as a non-member.
Leo654321 wrote:
can you overload operator without defining his classes (for his arguments)
I have no idea what you mean.
Leo654321 wrote:
HELP!
I'm trying!
Leo654321 wrote:
can you also give me another example of operator overloading?
Non-member version:
1
2
3
4
CVector operator+(CVector const &a, CVector const &b)
{
    return CVector(a.x+b.x, a.y+b.y);
}
The non-member version is always preferred over the member version.
Leo654321 wrote:
What is *(this) when should i use it and return it from function??
http://stackoverflow.com/q/2828841/1959975
Leo654321 wrote:
Thanks for the help!
You're welcome!
Leo654321 wrote:
answer will get gift
The best gift is knowing that you understand everything.
Last edited on
in this example in the function operator + the CVector temp is not initialized!

That's a flaw in the default constructor for CVector, which should initialise its data members to suitable defaults.

In the operator +, it's not a problem, because the values the data members of temphave values assigned to them immediately after construction, in lines 15 - 16.

how this code works ?

It's very straightforward. The two vectors being added are this and param. The result of the addition is stored in temp, and the operator then returns the results. So if we have:

1
2
3
4
5
CVector a(1, 2);
CVector b(3, 4);
CVector c;

c = a + b;


then line 5 is equivalent to:

c = a.operator+(b);

So a is this, b is param, and the contents of temp get copied into c.

why you need to write this fucntion as a member of class?

You don't. You can implement it as a free function too, if you prefer.

the left side gets copied to temp?

See above.

what will happen if you overload +operator? now the right side will get copied to the CVector temp inside the funnction??

Overloading the operator would have the same effect as overloading any function - it allows you to define another operator that takes different arguments, or have different const-ness. It won't change the fact that the left-hand operand becomes this, and the right-hand operand becomes param.

why you declare the operator + func outside the class? can you overload operator without defining his classes (for his arguments)
HELP!
can you also give me another example of operator overloading?
What is *(this) when should i use it and return it from function??

Are these homework questions? We're not a homework service.
Last edited on
1
2
3
4
CVector operator+(CVector const &a, CVector const &b)
{
    return CVector(a.x+b.x, a.y+b.y);
}

this is a function implementation that is outside the class. its not class member just regular function am i right?
does this fucntion takes two cvectos and return cvector that gets constructed using the sum of x,the sum of y.
how u implement this function??
how the return value is being used? when function ends this local var gets destoryed so its not good ? am i right?
thanks!
in this example in the function operator + the CVector temp is not initialized!

It is initialized. (it has a default constructor) & you changed it later!

also why you need to write this fucntion as a member of class?

It's a personal preference, you can write it as non-member if u want :
1
2
3
4
5
6
7
CVector operator+ ( const CVector& lhs, const CVector& rhs )
{
    CVector temp;
    temp.x = lhs.x + rhs.x;
    temp.y = lhs.y + rhs.y;
    return temp;
}


the left side gets copied to temp?

wut ?

what will happen if you overload +operator?

You can use operator + w/ ur defined class
that's why this piece of code worked
1
2
3
4
5
6
CVector foo (3,1);
CVector bar (1,2);
CVector result;
result = foo + bar; // if there is no definition(overload) for operator+
// for your CVector, then this piece of code will not work ( the compiler doesn't
 // know what to do with operator+ in this context 


now the right side will get copied to the CVector temp inside the funnction??

...

why you declare the operator + func outside the class?

it's not declared but rather defined, you can define it inside the class definition if you want.

can you overload operator without defining his classes (for his arguments)

no

What is *(this) when should i use it and return it from function??
this is a hidden pointer to an object of your class (it's passed as argument to a non static member function),
it's behavior is something like :
1
2
3
4
5
6
7
8
9
10
11
struct X {
    int data;
    void f( X* const this ) { } // it's defined something like this
};

int main()
{
    X x1;
    X* pX1 = &x1; // <== generated by compiler (might be)
    x1.f( /* pX1 */ ); // the compiler passes a hidden pointer to X something like this
}

so you can for instance use :
1
2
3
4
5
6
CVector CVector::operator+ (const CVector& param) {
  CVector temp;
  temp.x = this->x + param.x; // remember this is a pointer, to access the member u need to use ->
  temp.y = this->y + param.y;
  return temp;
}


*this (value pointed by this) is usually returned by assignment & compound assignment operators. (not limited to)

Where's my gift ??
Last edited on
> this is a function implementation ...
> how u implement this function??
like that.


> how the return value is being used?
by instance
1
2
3
4
5
//assume that a, b, c, d are CVector objects
c = a+b;
d = a+b+c;
c = 2*(a+b);
std::cout << a+b << ' ';
@MikeyBoy its not homework
can you explain the this. how is it being used.
and answer my above post

^
|
|
|
|

i appreciate your answers ..
Thnks!
1
2
3
4
CVector operator+(CVector const &a, CVector const &b)
{
return CVector(a.x+b.x, a.y+b.y);
}

this is a function implementation that is outside the class. its not class member just regular function am i right?
does this fucntion takes two cvectos and return cvector that gets constructed using the sum of x,the sum of y.
how u implement this function??
how the return value is being used? when function ends this local var gets destoryed so its not good ? am i right?
HOW CAN YOU RETURN LOCAL VARIABLES OF FUNCTION that is being destroyed when returning from function??????
HELP!!
Last edited on
> HOW CAN YOU RETURN LOCAL VARIABLES OF FUNCTION
> that is being destroyed when returning from function??????
http://networketiquette.net/core_rules_do_not_use_all_caps.html

To return the `foo' variable write return foo;
¿have you ever used a function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// overloading operators example
#include <iostream>

class CVector {

  public:

    // http://www.stroustrup.com/C++11FAQ.html#member-init
    int x = 0 ;
    int y = 0 ;

    CVector() = default ; // http://www.stroustrup.com/C++11FAQ.html#default
    CVector (int a,int b) : x(a), y(b) {}

    // for a class that is copy assignable, if operator+ is overloaded,
    // princile of least surprise implies: also overload operator +=
    // (this must be is usually overloaded as a member function)
    CVector& operator += ( const CVector& ) ;
};

CVector& CVector::operator+= ( const CVector& that ) {

  x += that.x ;
  y += that.y ;
  return *this ;
}

// then, it is canonical to implement operator+ as a non-friend, non-member function
// in terms of the already implemened operator+= (note: 'a' is passed by value)
CVector operator+ ( CVector a, const CVector& b ) { return a += b ; }

int main () {

  const CVector foo (3,1);
  const CVector bar (1,2);

  // http://www.stroustrup.com/C++11FAQ.html#auto
  const auto result = foo + bar;
  std::cout << result.x << ',' << result.y << '\n';
}
Last edited on
// (this must be overloaded as a member function)

operator+= can be non-member here, no private data to access.
Thanks Cubbi. (corrected).
@Cubbi: in that case the tutorial on this site needs to be updated as it has a table showing that the compound assignment operators can only be overloaded as member functions and not non-member functions. It's Clases II, I don't want to link to it at the moment because the tutorial links are all mismatched.
@LB cppreference.com is already more than I can handle. This site's admin is very responsive to "Spotted an error? Contact us", though.
I wish cppreference.com would add a "Tutorial: Newbie? Start learning C++ here" section.

It is already wiki in which an excellent group of C++ enthusiasts actively contribute content; in a short time, that tutorial could become one of the best learning resources on the web for beginners.
@JLBorges it did add that section: http://en.cppreference.com/book , but it was started only recently, and I am not sure there's the manpower to make it competitive soon.
Topic archived. No new replies allowed.