Re: Copy constructors
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:ru9q83dkqtvrte4i49cc2lduonf4lifa43@4ax.com...
On Thu, 5 Jul 2007 18:11:55 +0100, "David Webber"
<dave@musical-dot-demon-dot-co.uk> wrote:
Can someone remind me of the rules by which a default copy constructor is
provided?
I have a peculiar situation: a class for which I want to *forbid* a
copy
constructor of the form
(1) X::X( const X & );
but instead have something similar of the form
(2) X::X( const X&, const Y& );
I could write (1) and make sure it always ASSERTs, but, rather than do
that,
is there any way I can make the compiler throw an error if I inadvertently
try to call such a thing?
Most classes I create include the following:
class X
{
private:
// Copyguard
X(const X&);
void operator=(const X&);
};
Declaring these functions prevents the compiler from supplying default
versions. Declaring them private turns most attempted usage into
compile-time errors. Not defining them guarantees a link-time error if X
itself or a friend tries to use them.
Thanks Doug and David - like all the best answers simple and elegant!
[I must have a religious aversion to even thinking of declaring members and
not implementing them - time to overcome it <g>.]
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm