Overloading of assignment operator.

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

	String(const char* aChar){
		string = new char[strlen(aChar)+1];
		strcpy(string, aChar);

		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)];


		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

Question by:panJames
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
LVL 32

Assisted Solution

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.
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 38827474
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
LVL 86

Accepted Solution

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];


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

		return *this;

Open in new window


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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…

691 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