help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: private copy ctor with user-defined assign op


From: Dyre Tjeldvoll
Subject: Re: private copy ctor with user-defined assign op
Date: 23 Jul 2004 10:29:46 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

Bob Nelson <bnelson@nelsonbe.com> writes:

> Consider this program which compiles with no errors on GNU C++ 3.3.X
> and earlier. Why does GNU C++ 3.4.X require a public copy ctor in
> order to compile?

To be standard-conforming I guess. The compiler is allowed to create a
temporary (and so use the copy ctor) when calling operator=(const C
&rhs), with an r-value. And the standard says that if a temporary is
allowed, it MUST be possible to call the copy ctor even if this implementation
doesn't need it, (8.5.3.5).

> 
> ---------------- [cut] --------------
> class C {
> private:
>     C(const C &);
> public:
>     C(): i(0) {}
>     C(int i): i(i) {}
>     C &operator=(const C &rhs) {
>         this->i = rhs.i;
>         return *this;
>     }
> private:
>     int i;
> };
> 
> int main()
> {
>     C c;
> 
>     c  = C(42);
> }
> ------------ [cut] -----------------------
> 

-- 
dt

However, experience shows that for many people and many applications a
dose of paranoia is reasonable - Bjarne Stroustrup


reply via email to

[Prev in Thread] Current Thread [Next in Thread]