컴공 일기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를 선물하세요.
-
나 급해
-
만나자할때만날껄 1
저점매수실패 에휴이
-
고2 모의고사 기준으로 물1 , 지1 2등급인데 사문 + 지1 으로 바꾸는게...
-
화작 1 미적 1 영어 1 동사 1 정법 3입니다. 한의대는 절대 안될거...
-
우진희 2
N수생 farewelcome 진자 웃기네ㅋㅋㅋㅋ영어랑 섞어서 하니까 더 웃김
-
어제자 연응통 6
얘 컷 왜 이러죠
-
반갑네요
-
세종대식 2
700.14인데 AI로봇이나 공과계열 안되겟죠……
-
대학 다니다가 오랜만에 이번에 메디컬 이슈로 수능보게 됐는데 정시 원서를...
-
환승 최소 2번인데 할만할까요? 좀 걷기도 해야함
-
그냥 삼도극 저격인가요? 미분 적분 문제들은 솔직히 옛날이나 요즘이나 거기서 거기 같은데
-
올해 수능 미적 3 맞았습니다 고3때 내신으로 확통 개념기출은 했고 수1수2는...
-
공군 군수(2번 응시)vs무휴학 재수(과 생활x,최소 학점 수강,필요시 대치 단과...
C인가요?
C++ 이에용
이런 내용들은 어디서 배울 수 있는건가요...독학으로 하시는 건가요?
독학, 책이죠 뭐