Re: Compiler warning / copy constructor?
On Thu, 14 Feb 2008 11:24:51 +0100, "Gaute" <Gaute@nospam.nospam> wrote:
I want some classes to genereate a compiler waning/error when the (default)
copy-constructor is used.
Is there a way to do this?
For example:
class CMyClass {
public : CMyHugeData m_data;
};
I want the compiler to give a warning here or when the function is called:
void fooWarning( CMyClass xx ) {}
But no warning here:
void fooOk( CMyClass& xx ) {}
Best regards from Gaute
It's hard to imagine wanting to suppress the copy ctor but not the
assignment operator. In classes I don't want to support copying, I get rid
of both as follows:
class X
{
private: // Copyguard
X(const X&);
void operator=(const X&);
};
Of course, I don't implement the functions, so any error not caught at
compile-time will be caught at link-time. All classes of substance should
specify their copying semantics. If they don't support copying, what I've
shown above is sufficient. If the default functions are sufficient, say so
with a comment, and it's nice to do the same for the destructor.
I'm aware the Boost library comes with a class "noncopyable" that is
intended to be derived from. I wouldn't recommend using it. It's just an
ersatz (but portable) simulation of what MS would call "attributes", and it
relies on the empty base class optimization to avoid growing the class
size, which for VC, works for only the first class in the base class list.
It's also not a whole lot shorter to write than what I've shown. Finally,
it makes every class that uses it depend on Boost, and creating this sort
of library dependency for trivial things is something to avoid on general
principle.
--
Doug Harrison
Visual C++ MVP