检查char指针是否为null-Copy Constructor

Checking to see if a char pointer is null - Copy Constructor

本文关键字:null-Copy Constructor 是否 char 指针 检查      更新时间:2023-10-16

我正在编写以下代码

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;
}

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

if(mycharpointer != NULL)  

在基类中结果是真的?即使我在基类的初始化列表中显式地为它赋值为NULL。

那个检查太荒谬了。在构造时,当我们进入主体时,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;
}

或者,由于您已经在使用string,我们也可以将string用于mycharpointer。这还有一个额外的好处,我们甚至不必编写复制构造函数,正如你所看到的,它可能很容易出错:

base(const base& ) = default;

C++编译器只确保全局变量和静态变量将被初始化,因此在这种情况下,mycharpointer实际上可能指向一些无用的垃圾(悬挂指针)

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;
    }

由于mycharpointer实际上指向堆上分配的数据,因此如果您想重新定位它,则需要首先释放现有数据。类似于:

    if ( mycharpointer)  {
        delete [] mycharpointer;
}

显然,您希望默认构造函数在复制构造函数之前运行。

但不会。