Is it right to use declaration only for empty private constructors in C ++?

For example, this is correct:

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

or you must provide definition (s):

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

? Thanks for the current answers. Let me expand this question - does the compiler generate the best code in one of these examples? Can I imagine that providing the body for the ctor force compiler includes some (empty) code in the compilation unit? Is this also true for auto-generated code?

+4
source share
8 answers

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

+10
source

This is normal until you use them.

Using the 0x standard, you can use remote functions .

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

You can use constructors only for declarations to ban construct data such as standard construction or copying.

For example, if you want your object not to be copied, you can declare it as private with the copy constructor and assignment operator and not define them, then anyone, including you, who is trying to copy the object will have linker errors.

About your last edit: I expect a decent compiler to generate the same code for the default constructor and for the empty constructor and without an initialization list, in the end what he needs to do is initialize each element by default.

+5
source

If you declare them without providing an implementation, you cannot use them because they do not exist. If you want to use constructors, you must either allow the compiler to create them without declaring them, or you must declare them and provide an implementation.

It is sometimes useful to provide a declaration, but not implement a constructor that you do not want to use. This is often done using the copy constructor for objects (such as single objects) that you do not want their copies to ever be. In such cases, the declaration is often made private.

+2
source

First, if you want your class to not be completely copied, do not run the private copy constructor and assignment operator. Otherwise, it is still possible that a piece of code that has access (method or friend) can quietly make copies. Without implementation, you will get a linker error.

However, a compiler error is preferable because you will find out about the error faster. There is boost::noncopyable , or you can get a base class that hides its constructor and assignment operator.

Regarding the default constructor: the compiler will not generate it if you declare any constructor at all. Usually there is no need to hide it.

+2
source

If you use an "empty" declaration, the compiler will no longer generate a default implementation, and you will get link errors. If you declare them, you must write them so that you need a form with an empty bracket.

0
source

You will need to provide definitions. If you do not, and you try to use them, he will not be able to link.

0
source

It depends on whether you use these constructors or not. If you do not use them, you can leave them undefined. If you use them (for example, you create class objects from static class functions, you need to define them), you need to provide definitions, otherwise you will receive unresolved external character errors from the linker.

0
source

All Articles