Operator overloading

overloading of the += operator

Page 1 of 1

6 Replies - 816 Views - Last Post: 23 January 2010 - 06:28 AM Rate Topic: -----

#1 ben1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-January 10

Operator overloading

Posted 21 January 2010 - 01:59 PM

After much experimenting I finally found the correct way to implement
the +-, -=, and other operators as friend functions.

The left had side of the friend functions cannot have the const modifier.

This is because it could be modified. there is one weakness which I can see here,
if the class had more that a string a private variable, and one was going to have to operate on those
then operation would have to be implemented as a member function.

Chip


#include <iostream>
#include <cstdlib>

using namespace std;
class ll_node 
{
	public :
	ll_node() {next = 0;prev = 0; value="";}  
	ll_node(string val) {next = 0; prev=0;value=val;}
	ll_node(ll_node * n_ll_node,const string  & val){next=n_ll_node;prev=0;value=val;}
	int get_s();
	void set_s( int x);

	friend const string operator +(const ll_node &lhs, const ll_node &rhs);
	friend const string  operator +=(ll_node &lhs, const ll_node &rhs);
	 
	//  work with the next porinters
	void	 set_next(  ll_node * lln_next) {next=lln_next;};
	ll_node *get_next (void) const {return next;} 

	// previous pointe 
	void	 set_prev( ll_node * lln_prev){prev=lln_prev;};
	ll_node * get_prev( void) const {return prev;};

	//data operators
	void set_value(const  int & s){ value=s;}
	const string  get_value  (void) {return value;}
	void set_st(int i){s=i;}
	int get_st(int i){return s;}

	// 
	// private area for the linked list
	private:
	 string value;
	 ll_node *next;
	 ll_node *prev;
	static int s;
};
const string  operator +(const ll_node &lhs, const ll_node &rhs)
{
	return lhs.value+rhs.value;
}
 

const string  operator +=( ll_node &lhs, const ll_node &rhs)
{
	return lhs.value+=rhs.value;
}
 
int main ( int argc, char * argv[])
{

	ll_node cl1("15");
	ll_node cl2("bbbbb");
	cl1+=cl2;
	cout <<"cl1=+cl2 is "<<cl1.get_value()<<endl;
	
	return 0;

}

 


Is This A Good Question/Topic? 0
  • +

Replies To: Operator overloading

#2 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Operator overloading

Posted 21 January 2010 - 03:06 PM

One thing you could change is to have the operators return the same data type as the arguments, in your case ll_node. This will allow chaining of the operation like this:
cl1 + cl2 + cl3;


Was This Post Helpful? 1
  • +
  • -

#3 ben1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-January 10

Re: Operator overloading

Posted 21 January 2010 - 03:25 PM

Would it be possible to return a const reference to an ll_node, perhaps, so the new object does not have to be
copied.

Thanks

Chip
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Operator overloading

Posted 21 January 2010 - 03:35 PM

Yes, try it

Here's a reference-like page for operator overloading.

As there is written in that page, Compound Assignment Operators should be defined as member functions.
**Edited**

This post has been edited by Anarion: 21 January 2010 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Operator overloading

Posted 21 January 2010 - 03:38 PM

View Postben1991, on 21 Jan, 2010 - 02:25 PM, said:

Would it be possible to return a const reference to an ll_node, perhaps, so the new object does not have to be
copied.


It is possible, in principle, although I have a hard time understanding the logic of this example. You are really concatenating the strings stored in two nodes of a linked list. It doesn't seem to make sense to add two actual nodes.
Was This Post Helpful? 0
  • +
  • -

#6 ben1991  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 14-January 10

Re: Operator overloading

Posted 22 January 2010 - 08:14 PM

View PostPaul-, on 21 Jan, 2010 - 02:38 PM, said:

View Postben1991, on 21 Jan, 2010 - 02:25 PM, said:

Would it be possible to return a const reference to an ll_node, perhaps, so the new object does not have to be
copied.


It is possible, in principle, although I have a hard time understanding the logic of this example. You are really concatenating the strings stored in two nodes of a linked list. It doesn't seem to make sense to add two actual nodes.



I seem not to be able to post to the forum
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Operator overloading

Posted 23 January 2010 - 06:28 AM

Any operator which involves the = symbol is usually better off returning *this by-reference - and an assignment operation is a modifying operation in itself - so const'ness makes no sense whatsoever for the returned operand (which should be a reference to a newly modified left-hand operand).
class foo
{
    int x, y;
public:
    foo& operator += (const foo&); 
};

foo& foo::operator += (const foo& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
} 

This post has been edited by Bench: 23 January 2010 - 06:32 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1