检查char指针是否为null - 复制构造函数

Raj*_*war 1 c++ constructor initialization copy-constructor

我正在处理以下代码

class base
{
private:
    char* mycharpointer;
    std::string mystring;

public:
    base() : mycharpointer(NULL) {/*default constructor*/}

    //Copy Constructor
    base(const base& rhs){
        if(mycharpointer != NULL)  ---> Why is this condition true ?
        {
            mycharpointer = new char[ strlen(rhs.mycharpointer + 1)];
            strcpy(this->mycharpointer,rhs.mycharpointer);
        }
        mystring = rhs.mystring;
    }


    base operator=(base& b)
    {   
        if(this == &b) 
            return *this;

        base temp(b); 
        temp.swap(*this);
        return *this;
    }

    //Swap operation
    void swap(base& lhs) {
        std::swap(lhs.mycharpointer,this->mycharpointer);
        std::swap(lhs.mystring,this->mystring);
    }

    //Destructor
    virtual ~base(){
        if(mycharpointer) 
            delete[] mycharpointer;
    }
};

class der : public base
{
public:
    char* mycharpointer_der;
    std::string mystring_der;
    foo* f;

public:
    der():mycharpointer_der(NULL)
    {
    }


    der(const der& rhs) : base(rhs) 
    {
        if(mycharpointer_der) 
        {   
            mycharpointer_der = new char[ strlen(rhs.mycharpointer_der + 1)];
            strcpy(this->mycharpointer_der,rhs.mycharpointer_der); 
        }
        mystring_der = rhs.mystring_der;
        f = new foo(*rhs.f);
    }


    der& operator=(der& d)
    {   
        if(this == &d) //Make sure its not the same class
            return *this;

        base::operator= (d);
        der temp(d); 
        temp.swap(*this);
        return *this;
    }

    //Swap operation
    void swap(der& lhs) {
        std::swap(lhs.mycharpointer_der,this->mycharpointer_der);
        std::swap(lhs.mystring_der,this->mystring_der);
    }

    virtual ~der(){
         if(mycharpointer_der) //Necessary check as to make sure you are not deleting a NULL address otherwise exception thrown.
            delete[] mycharpointer_der;
    }

};



int main()
{
    der d;
    d.mycharpointer_der = "Hello World";
    d.mystring_der = "Hello String";
    der b;
    b = d;
}
Run Code Online (Sandbox Code Playgroud)

现在在上面的代码中调用d的复制赋值运算符.反过来调用基类的复制赋值运算符.在基类的复制赋值运算符中,调用基类的复制构造函数.我的问题是为什么是条件

if(mycharpointer != NULL)  
Run Code Online (Sandbox Code Playgroud)

在基类中变成真实的?即使我在基类的初始化列表中明确指定了NULL.

Bar*_*rry 6

那张支票太荒谬了.在建设的角度来看,当我们进入人体后,mycharpointer是默认初始化和将包含一些垃圾值可能0,但可能不会.

那就是说,如果rhs.mycharpointer是NULL 会发生什么?然后strlen呼叫将失败.这是charpointer你需要检查的价值:

base(const base& rhs)
{
    if (rhs.mycharpointer) {
        mycharpointer = new char[ strlen(rhs.mycharpointer) + 1 ]; 
        //                         outside the parens      ^^^^
        strcpy(this->mycharpointer,rhs.mycharpointer);
    }
    else {
        mycharpointer = NULL;
    }
    mystring = rhs.mystring;
}
Run Code Online (Sandbox Code Playgroud)

或者,因为你已经在使用string,我们就可以继续使用stringmycharpointer太多.这有一个额外的好处,我们甚至不必编写复制构造函数,正如您所见,它可能容易出错:

base(const base& ) = default;
Run Code Online (Sandbox Code Playgroud)