TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Basic MyString Move Semantics Implementation
Gaurav Kumar
Sep 17
2014
Code
1.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#ifndef MYSTRING_H
#define MYSTRING_H
#include <string>
using
namespace
std;
class
MyString
{
public
:
MyString()
{
m_ptr = nullptr;
}
explicit
MyString(
const
string str)
{
m_ptr =
new
string(str);
}
MyString(
const
MyString& other)
{
if
(other.m_ptr)
m_ptr =
new
string(*other.m_ptr);
else
m_ptr = nullptr;
}
MyString(MyString&& other)
{
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
}
MyString& operator =(
const
MyString& other)
{
MyString(other).swap(*
this
);
return
*
this
;
}
MyString& operator =(MyString&& other)
{
MyString(move(other)).swap(*
this
);
return
*
this
;
}
~MyString()
{
delete
m_ptr;
}
string get()
const
{
return
m_ptr ? *m_ptr : 0;
}
void
swap(MyString& other)
{
std::swap(
m_ptr, other.m_ptr);
}
private
:
std::string *m_ptr;
};
#endif // MYSTRING_H
c++11
copy constructor
move constructor
move semantics