컴공 일기259
게시글 주소: https://orbi.kr/00070852115
String 처리에 대한 객체 예제를 쭉 작성해보고 있습니다.
보잘 것 없지만 지원할 만한 것은 다 지원되는 듯 합니다… 구현되지 않은 기능들이 아직 많지만요.
이동시맨틱에, 딥 카피에, 각종 사칙 연산…
직관적인 편의성을 제공하는 객체로 변모해가는 중..
#pragma once
#include <iostream>
using namespace std;
class CMystring
{
public:
CMystring();
~CMystring();
//멤버 변수에 포인터가 있으므로 Deep Copy를 반드시 지원해야 한다.
CMystring(const CMystring&);
explicit CMystring(const char* pszData);
CMystring(CMystring&&) noexcept;
const char* getData() const;
void setData(const char*);
const size_t getLength() const;
CMystring& operator=(const CMystring& rhs);
CMystring& operator=(CMystring&& rhs) noexcept;
CMystring operator+(const CMystring& rhs);
size_t append(const char* param);
operator const char*(void) const;
private:
char*m_pszData = nullptr;
size_t length = 0;
};
CMystring::CMystring()
{
cout << "CMystring()" << endl;
}
//Deep Copy
CMystring::CMystring(const CMystring& rhs)
{
setData(rhs.m_pszData);
}
CMystring::CMystring(const char* pszData)
{
cout << "CMystring(const char*)" << endl;
setData(pszData);
}
CMystring::CMystring(CMystring&& rhs)
{
cout << "CMystring(CMystirng&&)" << endl;
delete m_pszData;
m_pszData = rhs.m_pszData; //shallow copy
this->length = rhs.length;
rhs.m_pszData = nullptr; //댕글링 포인터로 만들어준다.
}
CMystring::~CMystring()
{
cout << "~CMystring()" << endl;
delete[] m_pszData;
}
CMystring& CMystring::operator=(const CMystring& rhs)
{
this->setData(rhs.m_pszData);
return *this;
}
CMystring& CMystring::operator=(CMystring&& rhs)
{
cout << "opeartor=(CMystring&&)" << endl;
delete m_pszData;
m_pszData = rhs.m_pszData;
this->length = rhs.length;
rhs.m_pszData = nullptr;
return *this;
}
CMystring::operator const char*(void) const
{
return m_pszData;
}
const char* CMystring::getData() const
{
return m_pszData;
}
void CMystring::setData(const char* pParam)
{
//setData()가 여러번 호출될 경우, m_pszData가 null이 아닐 수도 있다.
if(m_pszData != nullptr)
delete[] m_pszData;
size_t length = strlen(pParam);
m_pszData = new char[length + 1];
this->length = length;
strcpy(m_pszData, pParam);
}
CMystring CMystring::operator+(const CMystring& rhs)
{
CMystring retVal(*this);
retVal.append(rhs.getData());
return retVal;
}
size_t CMystring::append(const char* param)
{
if(param == nullptr) return -1;
if(m_pszData == nullptr)
{
this->setData(param);
return this->length;
}
size_t lenAppend = strlen(param);
char* result = new char[length + lenAppend + 1];
strncpy(result, m_pszData, length+1);
result[length] = '\0';
strncat(result, param, lenAppend);
delete[] m_pszData;
m_pszData = result;
length += lenAppend;
return this->length;
}
const size_t CMystring::getLength() const
{
return this->length;
}
CMystring operator+(const char* pLeft, const CMystring& rhs)
{
CMystring result(pLeft);
result.append(rhs.getData());
return result;
}
0 XDK (+0)
유익한 글을 읽었다면 작성자에게 XDK를 선물하세요.
-
왜 여기선 믾은거 하라고 하죠..
-
막날까지 최종컷 0.7점까지는 오르면 그려려니할테니 붙여주
-
아 과외 미루지 말 걸 12
내가 미룬 게 아니긴 한데 안 가던 날에 가려니까 귀찮네
-
i의 취미생활 님이 만드신 모의고사 1회 손풀이(+빠진문제) 3
글씨가 더러울 수 있슴니다.. ㅋㅋ 현역분이 만드신거라고 믿기 힘들 정도로 퀄리티가...
-
1-1 1.33 1-2 1.14 2-1 1.28 (일본어 미포 1.13) 2-2...
-
계속 수능을 응시하면된다.
-
야심한 밤이네요 3
ㅇㅈ메타를 굴릴 시간이 왔다고 생각해요
-
이번수능 3등급 입니당
-
오겜2 스포 5
하면 제가 인수분해시켜버리겠음뇨
-
걷다가 얼겟어
-
여르비 ㅇㅈ 2
ㅗ
-
!배설! 4
ㅅㅅㅅ 씨@발존나 나 왜이렇게ㅜ병@신같음ㄹㅇ로존나 난 경계선 지능 장@애임...
-
사탐적성검사 2
예상이랑 크게 다르지 않네 경제>역사인 건 좀 예상 못함
-
스토리를보기만하고 축하는안해줘 ㅠㅠ
-
한의대 사탐런 0
삼수생임 한의대 노리는데 이번 수능 물리3 지구 높2 나옴 물리는 걍 버리고...
-
그 수능보신 분들 중에 뉴런신봉자 ㅈㄴ 많을 듯 30번 걍 3차함수 세실근합 쓰면...
-
이유가 뭔가요? 원래는 적정표본수로 50명 정도를 요구한 곳에서는 갑자기...
C인가요?
C++ 이에용
이런 내용들은 어디서 배울 수 있는건가요...독학으로 하시는 건가요?
독학, 책이죠 뭐