English 中文(简体)
Is it correct to use declaration only for empty private constructors in C++?
原标题:

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?

最佳回答

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.

问题回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签