构造函数的调用解决方案

构造函数的调用
//   Listing   11.6 Use   of   copy   constructor   to   return   an   object   from   a   function.

#include   <iostream>  
using   namespace   std;

class   String   {
    char   *str;                                 //   dynamically   allocated   char   array
    int   len;
    char*   allocate(const   char*   s)           //   private   function
    {   char   *p   =   new   char[len+1];   //   allocate   heap   memory   for   object
        if   (p==NULL)   exit(1);       //   test   for   success,   quit   if   no   luck
        strcpy(p,s);                                         //   copy   text   into   heap   memory
        return   p;   }                                 //   return   pointer   to   heap   memory
public:
    String   (int   length=0);               //   conversion/default   constructor
    String(const   char*);                             //   conversion   constructor
    String(const   String&   s);                     //   copy   constructor
  ~String   ();                                                 //   deallocate   dynamic   memory
    void   operator   +=   (const   String&);   //   concatenate   another   object
    void   modify(const   char*);                   //   change   the   array   contents
    bool   operator   ==   (const   String&)   const;   //   compare   contents  
    const   char*   show()   const;         //   return   a   pointer   to   the   array
  }   ;

String::String(int   length)
{   len   =   length;
    str   =   allocate( " ");             //   copy   empty   string   into   heap   memory
    cout   < <   "   Originate:     ' "   < <   str   < < " '\n ";   }

String::String(const   char*   s)
{   len   =   strlen(s);                           //   measure   length   of   incoming   text
    str   =   allocate(s);                                   //   allocate   space,   copy   text