首页 > 试题广场 >

考虑下面的类声明: class RQ1 { private

[问答题]
考虑下面的类声明:
class RQ1
{
private:
     char * st;           // point to C-style string
public:
     RQ1() { st = new char [1]; strcpy(st, " "); }
     RQ1(const char * s)
     {st = new char [strlen(s) + 1]; strcpy(st, s); }
     RQ1(const RQ1 & rq)
     {st = new char [strlen(rq,st) + 1]; strcpy(st, rq.st); }
     -RQ1(l {delete [] st);
      RQ & operateor=(const RQ & rq);
      // more stuff
};
如果想将char*st转换为使用string对象的声明。那么上面的哪些方法不再需要显式定义,是string对象自己就可以管理的?
推荐
#include <string>
using namespace std;
class RQ1
{
private:
     string st;          // a string object
public:
      RQ1() : st(" ") {}
      RQ1(const char * s) : st(s) {}
      -RQ1() {};
// more stuff
};
不再需要显式复制构造函数、析构程序和赋值运算符,因为string对象提供了自己的内存管理功能。
发表于 2018-09-03 20:35:51 回复(0)