Question

I have created a string-class that among other overloads the inserter << and extractor >>. But something goes wrong here despite I use a copy-constructor where I take a deep copy of the objects member-variable. I suspect I need to do the same in the function that overloads the inserter >> (cin) but I cannot figure out how.

What happends is that when the user is entering his name in the conselewindow the cout returns nothing. Here is the scenario in the console

Enter a name:

the user enter for instance Sally And the console outputs

your name is: 

that is nothing.

So my question:

  • Why is the string-array not left after calling the overloaded function?
  • should I take a deep copy here? And if - how do I implement it.
  • Any other reasons to this problem?

Extremely greatful for help!!!


#include <iostream>

using namespace std;

class String {
   int str_len;

public:
   char *string;
   // default constructor
String() {
    str_len = 100;
    string = new char[str_len]();
    cout << "initialiserat default-constructor!\n";
}
// destrcutor
~String() {
    delete [] string;
    string = 0;
}


// copy constructor
String (const String &string_obj) {

    str_len = string_obj.str_len;

    if (string_obj.string) {
        string = new char[str_len];
        strncpy(string, string_obj.string, str_len);
    } else {
        string = 0;
    }
}

// Overloading assignment-operator
String operator=(const String& string_obj) {

    if (this == &string_obj) {
        return *this;
    }
    delete [] string;

    str_len = string_obj.str_len;

    if(string_obj.string) {
        string = new char[str_len];
        strncpy(string, string_obj.string, str_len);
    } else {
        string = 0;
    }

    return *this;
}

};

// overloading inserter <<
ostream &operator<<(ostream &stream, String obj) {

    stream << obj.string;
    return stream;
}

// overloading extractor >>
istream &operator>>(istream &stream, String obj) {

   cout << "Enter a name: ";
   stream >> obj.string;

   return stream;
}


int main() {

   String string3;

   cin >> string3;

   cout << "your name is: " << string3 << endl;

   return 0;
}
Was it helpful?

Solution

This operator

istream &operator>>(istream &stream, String obj) {

   cout << "Enter a name: ";
   stream >> obj.string;

   return stream;
}

is invalid. Parameter String obj is a local variable of the function that will be destroyed after exiting the function. Define the operator as

istream &operator>>(istream &stream, String &obj) {

   cout << "Enter a name: ";
   stream >> obj.string;

   return stream;
}

Also define the output operator as

ostream &operator<<(ostream &stream, const String &obj) {

    stream << obj.string;
    return stream;
}

Take into account that it is not a good design of the class when data member string is defined as public. Any body can change it.

class String {
   int str_len;

public:
   char *string;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top