Solved

Overloading of assignment operator.

Posted on 2013-01-28
3
262 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now