Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Move constructor
Post Flair (click to view more posts with a particular flair)
Post Body

I am studying Sam!s teach yourself cpp in one hour a day. On page 245 it explains move semantics briefly and says this code should trigger move constructor but I couldn't get it to work. What am I missing here?

#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
private:
    char *buffer;

public:
    MyString(const char *initString) // constructor
    {
        buffer = NULL;
        cout << "Default constructor: creating new MyString" << endl;
        if (initString != NULL)
        {
            buffer = new char[strlen(initString)   1];
            strcpy(buffer, initString);
            cout << "buffer points to: 0x" << hex;
            cout << (unsigned int *)buffer << endl;
        }
    }
    MyString(const MyString &copySource) // Copy constructor
    {
        buffer = NULL;
        cout << "Copy constructor: copying from MyString" << endl;
        if (copySource.buffer != NULL)
        {
            // allocate own buffer
            buffer = new char[strlen(copySource.buffer)   1];
            // deep copy from the source into local buffer
            strcpy(buffer, copySource.buffer);
            cout << "buffer points to: 0x" << hex;
            cout << (unsigned int *)buffer << endl;
        }
    }

    MyString(MyString &&moveSource)
    {
        if (moveSource.buffer != NULL)
        {
            buffer = moveSource.buffer; // take ownership i.e. 'move'
            moveSource.buffer = NULL;
            // set the move source to NULL
        }
        std::cout << "moved" << std::endl;
    }

    // Destructor
    ~MyString()
    {
        cout << "Invoking destructor, clearing up" << endl;
        delete[] buffer;
    }
    int GetLength()
    {
        return strlen(buffer);
    }
    const char *GetString()
    {
        return buffer;
    }
};
void UseMyString(MyString str)
{
    cout << "String buffer in MyString is " << str.GetLength();
    cout << " characters long" << endl;
    cout << "buffer contains: " << str.GetString() << endl;
    return;
}

MyString Copy(MyString &source) // function
{
    MyString copyForReturn(source.GetString()); // create copy
    return copyForReturn;                       // return by value invokes copy constructor
}

int main()
{

    MyString sayHello("Hello World of C  ");
    MyString sayHelloAgain(Copy(sayHello)); // invokes 2x copy constructor before move constructor and must do one copy one move after move ctor but I couldn't observe that
    return 0;
}

Output:

Default constructor: creating new MyString
buffer points to: 0x0x556dd2cda2c0
Default constructor: creating new MyString
buffer points to: 0x0x556dd2cda2e0
Invoking destructor, clearing up
Invoking destructor, clearing up

No "moved" print in the output ?

Author
User Disabled
Account Strength
0%
Disabled 1 month ago
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
187
Link Karma
103
Comment Karma
84
Profile updated: 5 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago