1

a

没有为str,len制定默认值

b

str指向s,可能存在二次释放的问题;len 应该为strlen(s) + 1

c

没有为str分配内存,应使用new char[strlen(s) + 1]来分配

2

析构函数里没有将指针成员释放
复制和赋值时,直接使用浅复制,导致内存二次释放
构造函数和析构函数中new和delete不对应。


3

默认构造函数、析构函数、拷贝(复制)构造函数、赋值函数

4

class nifty
    {
    private:
        char personality[40];
        int talents;
    public:
        nifty();
        nifty(const char * s);
        friend ostream & operator<<(ostream& os, const nifty& n);
    };
    
    nifty::nifty()
    {
        personality[0] = '\0';
        talents = 0;
    }
    
    nifty::nifty(const char* s)
    {
        strcpy(personality, s);
        talents = 0;
    }
    
    ostream & operator<<(ostream& os, const nifty& n)
    {
        os << n.personality << ":" << n.talents;
        return os;
    }

5

a

1. Golfer ()
2. Golfer(const char * name, int g = 0)
3. Golfer(const char * name, int g = 0)
4. Golfer ()
5. Golfer (const Golfer & g)
6. Golfer(const char * name, int g = 0)
7. 默认的赋值运算符
8. 默认的赋值运算符

b

一个可以进行深度复制的赋值运算符。

标签: int, char, 构造函数, 动态分配, const, 赋值, 复习题, Golfer

相关文章推荐

添加新评论,含*的栏目为必填