⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soln9_1.cpp

📁 Wrox.Ivor.Hortons.Beginning.Visual.C.Plus.Plus.2008 With sourcecode
💻 CPP
字号:
// Soln 9_1

/*
  CBadClass initializes the member p in the contructor with the address passed as the argument.
  The argument to the constructor could be a string allocated on the heap that could be 
  destroyed externally. This would leave the object with an invalid pointer.
  The constructor should copy the argument and there should be a destructor to release the memory for p.
  It is also possible the argument to the constructor could be null.
  The default constructor does not initialize the members at all.
  The class would be better defined as below.

*/
#include <cstring>

class CBadClass
{
private:
   int len;
   char* p;
public:
  CBadClass(const char* str=0):len(0)
   {
     if(p)
     {
       len = strlen(str);
       p = new char[len+1];
       strcpy_s(p, len+1, str);
     }
   }
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -