Operator Overloading

Hello There,
This time I'm moving on to little more advanced concepts.

At my school we still use TurboC++, which does not support the string class natively and so I am not able to use its functions easily.

I started with operator overloading and realized that I could use the function strcat(), in order to make my own string class in Turboc++.
What I want to do is basically use the assignment operator '=' to assign strings to a variable just like in Visual C++.

The problem is I really cant figure out the definition of the overloading function, so if anyone could help me out, it would be really helpful.

Note - Please don't say "Just use Visual C++" as I have to create a project and I'm not allowed to use Any other compiler other than Borland
closed account (o3hC5Di1)
Hi there,

the syntax for overloading operator=() is the following:

1
2
3
4
my_string& mystring::operator=(const my_string& copy)
{
    // code here
}


Note that you return a reference, in order to be able to chain assignments like so:

a = b = c;

You could also do the following to allow for some_string = "text";:

1
2
3
4
my_string& mystring::operator=(const char[] copy)
{
    //copy the parameter to underlying c-string
}


Also note that it takes a const reference as a parameter. Make sure to check if you need to make the char array larger when you assign new content to a string, so that you don't run into memory problems.
Please do let us know if you need any more help.

All the best,
NwN
Last edited on
@NwN
Thanks For Helping Out,
If its not too much to ask you you help me out with the function prototype as well.
I was planning on passing two parameters as I have to use strcat() to do the operations. That function takes two arguments so how do I implement that.
I realized that I can use the strcat() function to make my own string class in Turbo C++.

How did you conclude that?
@Josue Molina

I'm Sorry I wasn't Clear Enough, My Bad :P.
This is what I meant by that
1
2
3
4
string::operator = (string & word, char *copy)
{
          strcat(word, copy);
}


Obviously this code doesn't work.
I want a version that does.
For it to work, it first needs to make logical sense.

strcat concatenates two null-terminated character arrays and stores the result in one of them.

The best I can deduce you want is the following behavior:

1
2
3
char a[14] = "Hello, ";
char b[7] = "world!";
a = b; // a = "Hello, world!\0" 

In other words, will the assignment operator append its RHS to its LHS?
closed account (o3hC5Di1)
Hi there,

I'm assuming you have a string class:

1
2
3
4
5
6
7
8
9
10
11
12
class my_string
{
    char* string_holder;

    public:
        my_string& operator=(const char[] copy) 
};

my_string& my_string::operator(const char[] copy)
{
    //check for sizes, make string_holder bigger if necessary, then make "copy" the content of string_holder
}


You mentioned you were making a string class yourself - so this is what I assumed. Are you implementing it differently?

All the best,
NwN
Last edited on
@Josue Molina

You Were Close Man but no thats not what I want to do.

1
2
3
4

string word;
word = "Hello, Thanks For Helping";


This is what I'm trying to do, but it is not possible natively in TurboC++, hence the operator overloading.
@NwN

Thats Quite Close man, however your version defeats the purpose of having to make a explicit string class, as I can use pointers instead.

To use my string I would have to do
string word;
word.string_holder = "BLABLA";

What I want is to make an exact version of the string Data Type with the same '+', '=', etc. functions in TurboC++.
I don't think you can overload the pre-existing operators for strings , char , ints , floats , ect...
*edit on a side note you mention turboc++ a lot but that is just the ide a very outdated one.
Last edited on
@giblit

Yes, I get that I Mention TurboC++ a lot but thats because the problem is exclusive to that IDE.

My project would be A Lot simpler if I could manage to get this overload done.
I am not allowed to use any other compiler or IDE in my school.
Something like this?

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
#include <iostream>

class String
{
	public:
		String( void );
		String( char *value );
		String & operator=( char *rhs );
		friend std::ostream & operator<<( std::ostream &stm , const String &str );
	private:
		char *value;
};
String::String( void ) : value( "Undefined" ){}
String::String( char *value ) : value( value ){}
String & String::operator=( char *rhs )
{
	value = rhs;
	return( *this );
}

std::ostream & operator<<( std::ostream &stm , const String &str )
{
	return( stm << str.value );
}

int main() {
	String s1 , s2( "Hello world!" );
	s1 = "Good bye world!";
	std::cout << s1 << std::endl << s2 << std::endl;
	return 0;
}
@giblit

Kinda Like Does My Job,
I'm Down To Understanding it now.

Thank You Everybody For Your Help.
Okay what parts do you not understand? I'll try and explain what everything does.

line 6 - 9 are prototypes
Line 6 and line 7 are the constructors; default and with value.
line 11 is what we store the value of the string in

line 13 we are initializing the value on a default constructor to undefined
line 14 we are initializing the value of the constructor to the value of the parameter

line 15 we are saying the lhs ( left hand side value or *this ) value is equal to the rhs( right hand side ) value. Then we return the lhs or *this value.

Line 21 we are returning a ostream object that has the value of our string.
So when we do cout << String... it outputs the string instead of an error

line 27 we create two objects, a default object and an initialized one.
line 28 we are setting the default value equal to another value
line 29 we are outputting the two Strings
@giblit

Holy Smoke,
Thanks Man, I was just wondering about the *this portion(Havent studied about this special pointer yet).

Last Question - At Line 18 where you type return *this;
can we do say this - return value;
I'm not the best at explaining but basically when you call a public function from a class Object say something like obj.output();

Then say you have private variables of obj as something like value.

Then inside your output function you want to access the value of your obj. You can either call this->value or value. This just refers to the object that is calling the function. so to dereference it we would put *this

We are returning a reference to a String in the function earlier so I put return( *this ) to return the lhs value of the operator =.

Basically this is a pointer to the current class instance. It is implictly being using without even putting this-> But if you don't want two of the same variable names you could use that to be safe.


http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr035.htm

http://en.cppreference.com/w/cpp/language/this

http://forums.devshed.com/c-programming-42/using-keyword-this-c-328396.html
@giblit

Thanks Man.
Thats Good Enough for me, I have a basic understanding now.
Looking into The Different posts you gave now.

Thanks so much for clearing this up for me.
Topic archived. No new replies allowed.