operator overloading (left or right)

Hi
I have a class string that overloads operator + to accept a char* (string) and adds it at class's (char*) pointer address; My problem is i have created the overload func that adds the string from the right; But how to define if the character string is added from the left?, how to specify that to the compiler so it knows when to use "left" operator func or when to use the "right" one?

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
41
42
43
44
45
46
#include<malloc.h>
#include<stdio.h>
class string{
public:
	char* str;
	void initialise(){
		str = (char*)malloc(1);
		*str = 0;
	}
	string(){};
	~string(){};
	int velicina(){int i;
		for(i = 0; str[i] != 0;i++);//size of class's str string;
		return i;}
	string& operator +(char *LijeviString){
		string temp;int vl,vs,i;
		for(vl=0;LijeviString[vl]!=0;vl++);//length of the LijeviString; 
		temp.initialise();
		vs = this->velicina();
		temp.str = (char*)realloc(temp.str,vl+vs);//new size of the result string

		for(i=0;i<vl;i++){
			temp.str[i] = LijeviString[i];}//filling
		for(i=0;i<vs;i++){
			temp.str[i+vl] = this->str[i];}//filling
		temp.str[i+vl] = 0;//end of string
		return temp;
	}
	string& operator =(char *Lstr){
		string temp;int i;
		temp.initialise();
		for(i=0;Lstr[i]!=0;i++);
		temp.str = (char*)realloc(temp.str,i);
		for(i=0;Lstr[i]!=0;i++){
			temp.str[i]=Lstr[i];}
		temp.str[i]=0;
		return temp;}

};
void main(){
	string s1;string* spokazivac;                                                             
	spokazivac  = &s1;
	s1.initialise();
	s1 = s1 + "string";
	s1 = s1 = "substring";
}


(ok..., this code does opposite, it takes a char string from the right and adds it to the class string on the left(i will reverse it when i make the operator from right;)
Ignore the operator = function(for now:D)
Last edited on
when + is overloaded as a member function, the class object (in this case, 'string') is always on the left side. If you want to put 'string' on the right side, you need to make a global function that overloads +. These functions are sometimes declared as friends of the class, but I find that unnecessary, as you can usually stick to public stuff. See code below.

Also, you need to return a 'string', not a 'string&' from the + operator. The reason why is because you're returning a temporary object, and that object loses scope (dies) as soon as the function returns, which means the reference returned is bad (because the object it points to no longer exists).

And of course, returning a string value instead of a string& reference means you need a copy constructor, which you don't have now.

And ew @ malloc. But whatever works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class string
{
public:
    string operator + (const char* str) const
    {
      string temp(*this);             // uses copy ctor
      // append 'str' to 'temp' here
      return temp;                    // uses copy ctor
    }
};

static string operator + (const char* l,const string& r)
{
  return string(l) + r;   // uses a ctor which accepts a 'const char*' initializer
                          //  also uses a operator + which has 'string' on both left and right side
}



Note that copying full strings every time you need to call the copy ctor (which is often) is inefficient. As such, common practice in string objects is to 'share' string data with other string objects, keeping track of how many strings are accessing the string data with a reference count. When one string needs to change the string data, it checks to see if it's sharing data with any other strings (by checking the reference count). If it is sharing, it makes a unique copy of the string so that it's no longer sharing and modifies that.

You don't have to do this, of course. But it's a bit more efficient.
Last edited on
Thanx,
I copied your example of "friend" function.
I'm doing something similar to string sharing in my program, but it always has to change the string data so i need to copy it from one location to new...
Tnx for the help.
Topic archived. No new replies allowed.