How do I make this C++ object non-copyable?

前端 未结 10 855
太阳男子
太阳男子 2020-11-28 04:36

See title.

I have:

class Foo {
   private:
     Foo();
   public:
     static Foo* create();
}

What need I do from here to make Fo

相关标签:
10条回答
  • 2020-11-28 05:05

    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.

    • Because they are 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...
    • Which leaves friends (and the class itself) for which the error will occur under the form of 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:

    • The object is effectively Uncopyable, even if polymorphism may not be that useful
    • Inheritance leads to EBO 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.

    0 讨论(0)
  • 2020-11-28 05:05

    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

    0 讨论(0)
  • 2020-11-28 05:06

    Make the copy constructor private.

    Foo(const Foo& src);
    

    You don't need to implement it, just declare it in the header file.

    0 讨论(0)
  • 2020-11-28 05:10

    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.

    0 讨论(0)
  • 2020-11-28 05:13
    #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.

    0 讨论(0)
  • 2020-11-28 05:14

    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.

    0 讨论(0)
提交回复
热议问题