Search code examples
c++c++11inheritanceconstructordeleted-functions

Inheritance of =delete functions


let's say I have a class named File. I want to disable the copy constructor for every son of File, for example TextFile.

Would doing something like this will still disable the copy constructor of TextFile?

class File {
public:
    File(const File& f) = delete;
};

class TextFile:public File {
public:
};

Or is this necessary in order for this to be disabled?

class File {
public:
    File(const File& f) = delete;
};

class TextFile:public File {
public:
    TextFile(const TextFile& tf) = delete;
};

Solution

  • Your first code block is all you need. Since File is not copyable when the compiler goes to generate the copy constructor for TextFile it will see that and implicitly delete it since it can't make a legal copy constructor.

    What it does not do though is to stop you from making your own copy constructor in the derived class. If you are okay with that then that is all you need.