Chris@King 2017-04-27 15:23 采纳率: 100%
浏览 1075
已采纳

关于类的默认构造函数的问题

这是我写的一个类,然后定义了MyString mystr;结果编译通过但是不能运行

class MyString {
friend ostream& operator<<(ostream& os, const MyString& s);
friend bool operator==(const MyString& s1, const MyString& s2);
friend bool operator<=(const MyString& s1, const MyString& s2);
friend bool operator>=(const MyString& s1, const MyString& s2);
friend bool operator!=(const MyString& s1, const MyString& s2);
friend bool operator< (const MyString& s1, const MyString& s2);
friend bool operator> (const MyString& s1, const MyString& s2);

public:
MyString()=default;
MyString(const char* s) ;
//copy constructor
MyString(const MyString& s2) ;
~MyString()
{
delete[] str;
}
int size()const {return strlen(str);}
MyString& append(const char*);
MyString& append(MyString&);
char& operatorconst { return str[n]; }
private:
char* str='\0';
};

  • 写回答

2条回答 默认 最新

  • wm_LJL 2017-04-28 03:26
    关注

    private:
    char* str='\0';
    首先没见过在类中这么初始化成员成员变量的。都是在构造函数中初始化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?