컴공 일기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를 선물하세요.
-
연애썰 6
-
확실히 2학기 와서 던지는 애들이 많아지니까 편한듯
-
언제쯤 될까요?? 오늘 밤이라도 가능? 다 퇴근했을려나…
-
가천대 5칸 0
수시6광탈에 수능좆망해서 집가까운 가천대가서 삼반수하려고 합니다 가천대식...
-
왜 일주일째 자퇴 최종 승인이 안 나냐 ㅡㅡ 이거 신입학 대학 등록금 내기 전까지...
-
밤샌다 8
내가 뒤@지더라도 이거 이해 다하고 뒤진다
-
경희대 국제학: 학점관리해서 적당한 공대 복전or전과 노려볼 생각. 영어 자신있음....
-
국어 공부는 양의 문제가 아닌 것 같음. 수학 공부랑은 결이 또 다른 느낌. 수학은...
-
외글vs가톨릭 0
외글 화학과랑 가톨릭대 데이터사이언스과 둘 중에 어떤 곳이 더 나을까요??
-
이병헌을 보면 인간성은 욕해도 연기력은 욕 못 한다는 게 이해가 감…ㅋㅋ 진짜 연기 ㄱㅆㅅㅌㅊ
C인가요?
C++ 이에용
이런 내용들은 어디서 배울 수 있는건가요...독학으로 하시는 건가요?
독학, 책이죠 뭐