Solved

Overloading of assignment operator.

Posted on 2013-01-28
3
264 Views
Last Modified: 2013-01-29
Hello experts!

Could you please tell me what is wrong with the code below?

Thank you!

#include <iostream>
#include <cstring>
using namespace std;

class String{
	char * string;

public:
	String(const char* aChar){
		string = new char[strlen(aChar)+1];
		strcpy(string, aChar);
		cout<<string<<endl;
	}

	~String(){
		delete string;
		string = 0;
	}

	char * getString(){
		return string;
	}

	void operator = (char * aString){
		strcpy(string, aString);
	}

	friend ostream& operator<< (ostream & aOstream, const String & aString);

	String operator=(String & rhs){
		delete this->string;
		this->string = new char[strlen(rhs.string)];

		cout<<rhs.string;

		strcpy(this->string, rhs.string);

		return *this;

	}

};

ostream& operator<< (ostream & aOstream, const String & aString){
	aOstream << aString.string;
	return aOstream;
}

int main(){
	String string1("kdjshfgkljs");
	String string2("skldjhfls");

	cout << string1 << string2;

	string2 = string1; //it does not seem to be working fine here
	cout << string2;

	return 0;
}

Open in new window

0
Comment
Question by:panJames
3 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 100 total points
ID: 38827472
>> string = new char[strlen(aChar)+1];
>> this->string = new char[strlen(rhs.string)];
I noticed that the 2nd line doesn't add 1.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 38827474
>http://www.cplusplus.com/reference/cstring/strcpy/
void operator = (char * aString){
		strcpy(string, aString);
	}

Open in new window

you need to initialize the variable "string" first, otherwise you copy to a "null" reference
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 38827544
Well, as the real stuff is already said, some remarks on the side - an overloaded 'operator=()' should always return a reference to the object that it assigns in order to be 'chained' as in

a = b = c;

Open in new window


and the input parameter to that operator should be const (for 'const correctness'), since it never is changed, so that should be

	String& operator = (const char * aString){
		strcpy(string, aString);

                return *this;
	}

	String& operator=(const String & rhs){
		delete this->string;
		this->string = new char[strlen(rhs.string) + 1];

		cout<<rhs.string;

		strcpy(this->string, rhs.string);

		return *this;
	}

Open in new window

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question