[LC++]Copy constructor etc

Mark Phillips mark at austrics.com.au
Thu Jul 26 18:52:05 UTC 2001


Ram wrote:

> --- Mark Phillips <mark at austrics.com.au> wrote:
> > "John F. Dumas" wrote:
>
> > I am trying to do this with the string class but I
> > can't get it working yet.  What I have tried is:
> >
> > class noisyStringTy: public string {
> > public:
> >   noisyStringTy& operator=(noisyStringTy const& ns)
> > {
> >     return string::operator=(ns);
> >     cout<<"operator="<<endl;
> >   }
> >   noisyStringTy(): string() { cout <<
> > "noisyStringTy()" << endl; }
> >   noisyStringTy(char const* cStr): string(cStr) {
> >     cout<<"noisyStringTy(char const* cStr)"<<endl;
> >   }
> > };
> >
>
> I am not sure if we can call a operator = directly as
> above ? I also got the compilation errors.Does anyone
> know how to do the above ?
>

Yes, someone emailed me the correct way.  There are two
options:

  noisyStringTy& operator=(noisyStringTy const& ns) {
    cout<<"In noisyStringTy& operator=(noisyStringTy const& ns)"
        <<" --- assignment op"<<endl;
    *((string *)this) = ns;
    return(*this);
  }

Alternatively you can do this:

  noisyStringTy& operator=(noisyStringTy const& ns) {
    cout<<"In noisyStringTy& operator=(noisyStringTy const& ns)"
        <<" --- assignment op"<<endl;
    string::operator=(ns);
    return(*this);
  }

Here is the full version of the noisyStringTy:


class noisyStringTy: public string {
public:
  noisyStringTy(): string() {
    cout<<"In noisyStringTy() --- no-argument constructor"<<endl;
  }
  noisyStringTy(noisyStringTy const &ns): string(ns) {
    cout<<"In noisyStringTy(noisyStringTy const &ns) --- copy constructor"
        <<endl;
    cout<<"  string is \""<<*this<<"\""<<endl;
  }
  noisyStringTy(char const* cStr): string(cStr) {
    cout<<"In noisyStringTy(char const* cStr) --- C string
constructor"<<endl;
    cout<<"  string is \""<<*this<<"\""<<endl;
  }
  ~noisyStringTy() {
    cout<<"In ~noisyStringTy()\n";
    cout<<"  string was \""<<*this<<"\""<<endl;
  }
  noisyStringTy& operator=(noisyStringTy const& ns) {
    cout<<"In noisyStringTy& operator=(noisyStringTy const& ns)"
        <<" --- assignment op"<<endl;
    cout<<"  left string starts as \""<<*this<<"\""<<endl;
    *((string *)this) = ns;
    cout<<"  and finishes as       \""<<*this<<"\""<<endl;
    return(*this);
  }
};


Then you can play around with the following kind of things:


void doit1(noisyStringTy ns) {
}


void doit2(noisyStringTy const &ns) {
}


main(int argc, char **argv) {
  noisyStringTy ns="Hey there, Georgie girl!";
  noisyStringTy ns2;
  ns2=ns;
  ns2="Hi ho the Lone Ranger!!";
  cout<<"About to create n1 and n2\n";
  noisyStringTy n1;
  noisyStringTy n2;
  cout<<"Assigning n1=n2;\n";
  n1 = n2;
  cout<<"About to doit1\n";
  doit1(n1);
  cout<<"About to doit2\n";
  doit2(n1);
  cout<<"About to noisyStringTy n3(n2);\n";
  noisyStringTy n3(n2);
  return(0);
}


Hope this helps.  It helped me!

Cheers,

Mark.





More information about the tuxCPProgramming mailing list