注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

移动构造  

2014-05-19 22:53:41|  分类: 移动构造函数c+++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#include <iostream>
using namespace std;

class MyString
{
private:
char* Buffer;

// private default constructor
MyString(): Buffer(NULL)
{
cout << "Default constructor called" << endl;
}

public:
// Destructor
~MyString()
{
if (Buffer != NULL)
delete [] Buffer;
}

int GetLength()
{
return strlen(Buffer);
}

operator const char*()
{
return Buffer;
}

MyString operator+ (const MyString& AddThis)
{
cout << "operator+ called: " << endl;
MyString NewString;

if (AddThis.Buffer != NULL)
{
NewString.Buffer = new char[GetLength() + strlen(AddThis.Buffer) + 1];
strcpy(NewString.Buffer, Buffer);
strcat(NewString.Buffer, AddThis.Buffer);
}

return NewString;
}

// constructor
MyString(const char* InitialInput)
{
cout << "Constructor called for: " << InitialInput << endl;
if(InitialInput != NULL)
{
Buffer = new char [strlen(InitialInput) + 1];
strcpy(Buffer, InitialInput);
}
else
Buffer = NULL;
}

// Copy constructor
MyString(const MyString& CopySource)
{
cout << "Copy constructor to copy from: " << CopySource.Buffer << endl;
if(CopySource.Buffer != NULL)
{
// ensure deep copy by first allocating own buffer
Buffer = new char [strlen(CopySource.Buffer) + 1];

// copy from the source into local buffer
strcpy(Buffer, CopySource.Buffer);
}
else
Buffer = NULL;
}

// Copy assignment operator
MyString& operator= (const MyString& CopySource)
{
cout<<"Copy assignment operator to copy from: "<<CopySource.Buffer<<endl;
if ((this != &CopySource) && (CopySource.Buffer != NULL))
{
if (Buffer != NULL)
delete[] Buffer;

// ensure deep copy by first allocating own buffer
Buffer = new char [strlen(CopySource.Buffer) + 1];

// copy from the source into local buffer
strcpy(Buffer, CopySource.Buffer);
}

return *this;
}

// move constructor
MyString(MyString&& MoveSource)
{
cout << "Move constructor to move from: " << MoveSource.Buffer << endl;
if(MoveSource.Buffer != NULL)
{
Buffer = MoveSource.Buffer; // take ownership i.e. 'move'
MoveSource.Buffer = NULL; // free move source
}
}

// move assignment operator
MyString& operator= (MyString&& MoveSource)
{
cout<<"Move assignment operator to move from: "<<MoveSource.Buffer<<endl;
if((MoveSource.Buffer != NULL) && (this != &MoveSource))
{
delete Buffer; // release own buffer

Buffer = MoveSource.Buffer; // take ownership i.e. 'move'
MoveSource.Buffer = NULL; // free move source
}

return *this;
}
};

int main()
{
MyString Hello("Hello ");
MyString World("World");
MyString CPP(" of C++");

MyString sayHelloAgain ("overwrite this");
sayHelloAgain = Hello + World + CPP;

return 0;
}

/*
Without move constructor and move assignment operator:
Constructor called for: Hello
Constructor called for: World
Constructor called for: of C++
Constructor called for: overwrite this
operator+ called:
Default constructor called
Copy constructor to copy from: Hello World
operator+ called:
Default constructor called
Copy constructor to copy from: Hello World of C++
Copy assignment operator to copy from: Hello World of C++

With move constructor and move assignment operators:
Constructor called for: Hello
Constructor called for: World
Constructor called for: of C++
Constructor called for: overwrite this
operator+ called:
Default constructor called
Move constructor to move from: Hello World
operator+ called:
Default constructor called
Move constructor to move from: Hello World of C++
Move assignment operator to move from: Hello World of C++
*/


  评论这张
 
阅读(6)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017