[LC++]Copy constructor etc

Mark Phillips mark at austrics.com.au
Wed Jul 25 12:48:04 UTC 2001


"John F. Dumas" wrote:

[some excellent explanations snipped]

> By the way, these issues trouble all new C++ programmers - as it turns
> out probably the best way to get all this stuff really straight it
> so make little hello-world programs that show exactly what's going on
> via print statements as in:
>
> #include <iostream.h>
>
> class foo
> {
>    public:
>       foo()                       { cout << "foo()"  << endl;           }
>       ~foo()                      { cout << "~foo()" << endl;           }
>       foo(const foo &)            { cout << "foo(const foo &)" << endl; }
>       foo &operator=(const foo &) { cout << "foo::operator=" << endl;   }
> };
>
> then just try things out ...
>
> foo f1;
> foo f2;
> foo f3 = f1;
> foo f4(f1);
> f1 = f2;
>

Thanks for this!  I tried the above and found it most
informative.

> With respect to vector<int> you can do the same thing by just wrapping
> vector<int> inside a noisy subclass

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;
  }
};

Unfortunately, the "operator=" doesn't compile.  I tried
putting casts in there, ie
    return (noisyStringTy) string::operator=((string)ns);
but it still won't compile.

Any ideas about what I'm doing wrong?  I am trying to
get a "string" class which is noisy in that it tells you
what is being called as it does it.

Thanks,

Mark.



More information about the tuxCPProgramming mailing list