derived::derived(const derived&)' is implicitly deleted because the default definition would be ill-formed:

why is it that when i derived from a base class w/ deleted operator= and copy c'tor, the derived class' operator= and copy c'tor is also implicitly deleted ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class non_copyable {
public :
    non_copyable( const non_copyable& ) = delete;
    non_copyable& operator= ( const non_copyable& ) = delete;
protected :
    non_copyable() {}
};

class derived : public non_copyable {
public :
    derived( int i = 0) : i(i) {}
    int i;
};

int main () {
    derived foo;
    
    derived bar = foo;
}
Last edited on
Because if you there's no way to copy/assign the base class contents, then any attempt to copy/assign the derived class contents would be meaningless. You'd have the semantics of copying a class, but in fact you'd only be copying part of it, leaving you with a class in a partially-initialised and probably invalid state.
Topic archived. No new replies allowed.