Re: Copy constructors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sun, 8 Jul 2007 09:24:45 +0100, "David Webber"
<dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:


"Mateusz Loskot" <mloskot@xxxxxxxxx> wrote in message
news:op.tu3s98g1bk1lju@xxxxxxxxxxxxxxxxx

You've got already it well explained, so I'm only giving
yet another possible solution:

// http://www.boost.org
#include <boost/utility.hpp>

class A : boost::noncopyable
{
};

and you do not need to add any extra code to definition of A.

Thanks! One of these days I must explore the "boost" library, as I have
seen it recommended a few times.

But the simple trick of declaring, but not implementing, a copy constructor
appeals to me as a very simple procedure entirely within the C++ standard.
I have now used this to fix up my problems and it works fine.

I expect the boost::noncopyable class does the same thing, as the compiler
can't generate the defaults in derived classes if the base class versions
are inaccessible. It should even be slightly superior to what I presented,
since attempts by "A" and its friends to makes copies of "A" would be
diagnosed at compile-time instead of link-time. On the other hand, I
wouldn't want to use the boost method with a compiler that doesn't support
the empty base class optimization. All that said, the boost facility is
still cumbersome. Given that I make most of the classes I write
non-copyable, I wish the language would address this directly.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: C# Override limitation..why?
    ... > override so why can't it look for base.whateverin the base class? ... > look for methods in the base class where the signature parameters match ... It's not that the compiler ... static dispatch based on expression type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: the virtual keyword
    ... > you could not declare a body for it in the class it was declared virtual, ... This class can only be used as a base class, ... would result in a compiler error. ... function pointer that is added to the actual class. ...
    (comp.lang.cpp)
  • Compiler Error Message: BC31019
    ... Compiler Warning Messages: ... Warning: BC40003: property 'Profile' shadows an overloadable member ... overloadable member declared in the base class 'Default2'. ... to overload the base method, this method must be declared 'Overloads'. ...
    (microsoft.public.inetserver.iis.security)
  • Re: Reference to an undefined assignment operator of a base class in a derived class.
    ... As you can see in the base class there is no an assignment operator defined ... However in the derived class there is explicit reference to the ... The Microsoft compiler VC++ 2005 EE compiles the code without any error. ...
    (microsoft.public.vc.language)
  • preprocessor implementation GURU question
    ... I'm trying to resolve a disagreement between friends --Digital Mars ... The problem arose when I was trying to compile a boost example program ... compiler shouldn't be adding a space. ... involve concatenation, as it does not cause tokens to merge. ...
    (comp.lang.cpp)