Solved

Overloading of assignment operator.

Posted on 2013-01-28
3
263 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

22 Experts available now in Live!

Get 1:1 Help Now