Question

For example is this correct:

class C 
{
   private: 
     C();
     C(const & C other);
}

or you should rather provide definition(s):

class C 
{
   private: 
     C() {};
     C(const & C other) {};
}

? Thanks for the current answers. Let's extend this question - does compiler generate better code in one of this examples? I can imagine that providing body for ctor forces compiler to include some (empty) code in compilation unit? Is this also true for auto-generated code?

Was it helpful?

Solution

If you do not wish your object to be copyable, then there is no need to provide the implementation. Just declare the copy ctor private without any implementation. The same holds for other ctors, if you do not want any body to use them, just declare them private without any implementation.

OTHER TIPS

It is fine as long as you don't use them.

With the 0x standard you can use deleted functions.

class X {
   // ...
   X& operator=(const X&) = delete; // Disallow copying
   X(const X&) = delete;
};

You may use declaration-only constructors to disallow given constructions, like standard construction, or copy-construction.

For example, if you want to avoid your object being copied you can declare as private the copy-constructor and the assignment operator and do not define them, then anyone,including you, who tries to copy the object will have linker errors.

About your last edit: I'd expect a decent compiler to produce the same code for a default constructor and for an empty constructor and no initialization list, in the end, what it needs to do is default-initialize each member.

If you declare them without providing implementation, then you can't use them because they don't exist. If you want to use the constructors, you must either allow the compiler to create them by not declaring them, OR you must declare them and provide an implementation.

It is occasionally useful to provide a declaration but no implementation for a constructor that you don't want used. This is often done with the copy constructor for objects (such as singletons) that you don't want there to be copies of, ever. In such cases, the declaration is often made private as well.

Firstly, if you want to make your class completely non-copyable, don't implement the private copy constructor and assignment operator. Otherwise it is still possible that a piece of code that has access (a method or a friend) can silently make copies. Without the implementation, you'll get a linker error.

However, a compiler error would be preferable, as you'll find out about the error faster. For this there's boost::noncopyable, or you can derive from a base class that hides its copy constructor and assignment operator.

Concerning the default constructor: the compiler will not generate one if you declare any constructor at all. There's generally no need to hide that specifically.

If you use the 'empty' declaration, the compiler will no longer generate the default implementation and you will get link errors. If you declare them, you have to write them so the empty-brace form is necessary.

You will need to provide definitions. If you do not, and you attempt to use them, it will fail to link.

That depends on whether you use these constructors or not. If you don't use them you may leave them undefined. If you use them (for example you are creating objects of the class from static functions of class, you need to define them) you need to provide definitions, otherwise you'll get unresolved external symbols error from linker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top