See title.
I have:
class Foo {
private:
Foo();
public:
static Foo* create();
}
What need I do from here to make Fo
To add a bit there.
The traditional solution is, as has been said, to declare both Copy Constructor
and Assignment Operator
as private
, and not to define them.
private
, this will lead to a compile-time error from anyone trying to use them that has not access to the private parts of the class...undefined symbol
, either at link-time (if you check for those there) or most probably at run-time (when trying to load the library).Of course, it is quite a bother in the second case because you then have to check your code by yourself since you do not have the indication of the file and line at which the error occurs. Fortunately it's limited to your class methods and friends.
Also, it is worth noting that these properties are transitive down the inheritance and composition road: the compiler will only generate default versions of the Default Constructor
, the Copy Constructor
, the Assignment Operator
and the Destructor
if it may.
This means that for any of those four, they are automatically generated only if they are accessible for all the bases and attributes of the class.
// What does boost::noncopyable looks like >
class Uncopyable {
public:
Uncopyable() {}
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
This is why inheriting from this class (or using it as an attribute) will effectively prevents your own class to be copyable or assignable unless you define those operators yourself.
Generally inheritance is chosen over composition there for 2 reasons:
Uncopyable
, even if polymorphism may not be that usefulEBO
or Empty Base Optimization
, while an attribute will be addressable and thus will occupy memory (in each instance of the class) even if it does not actually need it, the compiler has the possibility not to add this overhead for a base class.You could, alternatively, declare the operators private and not define them in your own class, but the code would be less self-documenting, and you would not be able to automatically search for those class that have this property then (unless you have a full-blown parser).
Hope this shed some light on the mechanism.
The good practice in C++11 is to declare the copy constructor and assignment as publicly deleted. Not privately deleted, publicly deleted: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-delete
Make the copy constructor private.
Foo(const Foo& src);
You don't need to implement it, just declare it in the header file.
The typical way to make a C++ object non-copyable is to explicitly declare a copy constructor and copy-assignment operator but not implement them. This will prevent the compiler from generating its own. (Typically this is done in conjunction with declaring them private
so that it generates a compilation error instead of a linker error.)
There also is the boost::noncopyable class that you can inherit from, which does what I described above.
#include <boost/utility.hpp>
class Foo : boost::noncopyable {...
But as Scott Meyers once said..."It's a fine class, it's just that I find the name a bit un, err non natural", or something like that.
In C++11, you can explicitly disable the creation of default copy and assignment constructor by placing = delete
after the declaration.
From Wikipedia:
struct NonCopyable {
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable & operator=(const NonCopyable&) = delete;
};
The same goes for classes of course.