?
Solved

Overloading of assignment operator.

Posted on 2013-01-28
3
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 300 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 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 900 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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++.
Suggested Courses

752 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