컴공 일기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 1
언매 : 36.44틀 확통 : 15.21.22틀
-
지금 기하런은 비추임 기하에서 가장 중요한 것은 감각, 순간판단, 자유도임 지금...
-
만점의 벽은 4 1
도대체 얼마나 높은걸까요... 97까진 왔는데 딱 하나 걸려서 100점이 안 나오네
-
국어실력 떨어진게 확 체감됨 4 1
내신은 문학이라서 비문학 몇달동안 하나도 안 풀었더니 비문학 절반을 틀림...
-
독학기숙 수만휘vs잇올 ㅇㄷ가 0 0
나음?
-
기분 좋은 소식 0 0
중국 월드컵 탈락ㅋㅋㅋ
-
오지훈t 매개완 관련 질문이요 1 0
지구런해서 오지훈t 들으려는데 예전에 내신으로 들으려고 사놨던 2024매개완 들어도...
-
6모가 망했다면 3 3
6모때 망해서 다행인거고 이때한 실수 안하려고 노력하면돼요 잘봤다면 그것도 잘한거구
-
수학 20번 ㄹㅈㄷ 2 1
가 나 다 세개 다 더해야되는데 (다) 를 ㅈㄴ빨리구했다는 희열감때문에 80으로 씀 ㅋㅋㅋㅋㅋㅋ
-
정답률 개웃기네 ㅋㅋㅋㅋ 7 1
도형이라고 미적이랑 정답률 비슷하네
-
ㄹㅇ;
-
동점으로 시점 통일 내지는 고정되지 않은 벡터(PQ)를 성분화하는 게 익숙한...
-
20번 박스 안 줬으면 9 0
정답률 몇퍼?
-
생1 서바vs강k 3 0
올해는 하나 들어보려 하는데 어디가 더 괜찮나요 요즘은 강k도 잘나온다 해서.....
-
과외생 국수탐 2틀/2틀/50/50인데 (저도 같이 현장응시 1틀/1틀/50/50)...
-
의외로 요즘 안보이는 글 6 1
연예인 프사 단 사람이랑 얘기하면 연예인이랑 얘기하는 기분 어쩌고
C인가요?
C++ 이에용
이런 내용들은 어디서 배울 수 있는건가요...독학으로 하시는 건가요?
독학, 책이죠 뭐