0

I am poking around overloaded operators.

For some reason I don't get cout output from within my overloaded function.

class MyString {

    public:
        MyString(const char aString[20]){
            // copy the input string to "data"
            for(int i = 0; i < 20; i++){
                data[i] = aString[i];
            }
        }

    public:

        MyString operator=(const MyString copyFrom){
            MyString copyTo("");

            cout << "hi";

            for(int i = 0; i < 20; i++){
                copyTo.data[i] = copyFrom.data[i];
            }

            return copyTo;
        }

    public:
        char data[20]; // a pointer to memory
};


int main() {
    MyString a("hello");

    MyString b = a;

    cout << b.data << endl;

    return 0;
}

When I run my code, I get the following result:

C:\MinGW\bin>g++ stringoverloading3.cpp

C:\MinGW\bin>a.exe hello

C:\MinGW\bin>

Is there something about overloading that kills cout?

JoeBass
  • 519
  • 7
  • 18
  • 1
    unless of course my overload isn't actually getting called... hmm – JoeBass Feb 19 '17 at 23:36
  • 4
    This `MyString b = a;` is not an assignment (it's an initialisation), and so won't call the assignment operator. Your C++ text book should explain this. –  Feb 19 '17 at 23:37
  • 1
    btw arrays and pointers are not the same as the comment in the code suggests – 463035818_is_not_an_ai Feb 19 '17 at 23:40
  • And the assignment operator should take the parameter as a `const` reference. – Sam Varshavchik Feb 19 '17 at 23:44
  • This line `MyString b = a;` calls the copy constructor, not the assignment operator. – PaulMcKenzie Feb 19 '17 at 23:44
  • 2
    Your assignment operator (which isn't actually called) doesn't change the instance that was assigned. And your constructor reads past the end of its input buffer which is undefined behavior. – interjay Feb 19 '17 at 23:46
  • Your code has undefined behaviour, since it accesses an array out of bounds. You cannot pass arrays as prvalues in C++ (as we [just discussed](http://stackoverflow.com/a/42332266)). – Kerrek SB Feb 19 '17 at 23:50
  • @PaulMcKenzie What makes you say that? IMHO, poking around is a good way to find new things to learn. Old proverb: You don't know if you understood until you coded it and it works. Reading specifications is the last resort, not the way to start out learning new languages. Poking is good ;) – BitTickler Feb 19 '17 at 23:55
  • @BitTickler There are plenty of reputable books and websites that explain these things -- there is no need to read the specification. The issue with C++ is that you can "poke around", think you found something that works when it really doesn't work, and stick with this wrong code until it comes back to bite you. – PaulMcKenzie Feb 19 '17 at 23:58

1 Answers1

2

The line

MyString b = a;

is not an assignment. It is initialization. The copy constructor is called to initialize the object. To invoke the assignment operator, use:

MyString b;
b = a;

In order to be able to use that, the default constructor has to be implemented first.

R Sahu
  • 204,454
  • 14
  • 159
  • 270