C ++ 객체 지향 프로그래밍 노트 세

포인터 멤버 클래스

1. 세 가지 특수 기능

복사 생성자, 복사 할당, 소멸자

2.ctor 및 dtor

inline String::String(const char* cstr = 0)
{
    if(cstr)
    {
        m_data = new char[strlen(cstr)+1];
        strcpy(m_data, cstr);
    }
    else
    {
        m_data = new char[1];
        *m_data = '\0';
    }
}

inline String::~String()
{
    delete[] m_data;
}

{
    String s1();
    String s2("hello");

    String* p = new String("hello");
    delete p;
}

3. 포인터 클래스는 복사 생성자와 복사 할당이 있어야합니다 (사용자 정의)

깊고 얕은 복사 사본

inline String& String::operator=(const String& str)
{
    if(this == &str)
        return *this;
    
    delete[] m_data;
    m_data = new char[strlen(str.m_data) + 1];
    strcpy(m_data, str.m_data);
    return *this;
}

 

게시 44 개 원래 기사 · 원 찬양 5 · 조회수 1395

추천

출처blog.csdn.net/qq_33776188/article/details/104643523