C ++ Inheriting a private copy constructor: how does this not lead to a compile-time error?

In C ++, if we have this class

class Uncopyable{
public:
    Uncopyable(){}
    ~Uncopyable(){}
private:
    Uncopyable(const Uncopyable&);
    Uncopyable& operator=(const Uncopyable&);
};

and then we have a derived class

class Dervied: private Uncopyable{
};

My question is: why doesn't this raise a compile-time error when the compiler generates a default copy constructor and assignment operators in a derived class? Does the non-generated code try to access the private members of the base class?

+4
source share
6 answers

why doesn't this lead to a compilation error when the compiler generates a default copy constructor and assignment operators in a derived class?

, . , , / , , .

+3

, , , .

/, , / (.. , ). .

-:

  • ( )

  • , . .

  • , , .

  • , . .

  • , , .

  • Destructor

.

+2

++ 11 12.8/7 : " , ". Dervied . 12.8/11 :

copy/move inline public . / X (8.4.3), X :

  • X ,

  • M ( ), /, (13.3) M , ,

  • B, /, (13.3) B , , ,

  • , ,

  • , rvalue

  • , , .

, : Dervied Uncopyable, , Dervied:: Dervied(const Dervied&). Dervied , , .

+2

private private Derived, , , Derived, .

+1

, , . - Uncopyable Derived.

, , std::vector. , , push_back . -, , , , , . . , , , .

- , , , , . , , , :

Derived Foo;
Derived Bar;
Foo = Bar

:

int GetAnswer(Derived bar)
    { /* Do something with a Derived */ }
Derived Foo;
int answer = GetAnser(Foo);

, foo GetAnswer.

, - . , , , , opengl, .. , , . , , .

: Uncopyable , . :

int GetAnswer(Derived& bar)
    { /* Do something with a Derived */ }
Derived Foo;
int answer = GetAnser(Foo);

, , . - , , .

: , , , , - .

, . , umakeable. , , , . , , , . , ++.

, , - - .

class uncreateable
{
    uncreateable() {}
  public:
    static int GetImportantAnswer();
};

Looking at this, you can see that I do not need to instantiate the class to call GetImportantAnswer (), but I could not create the instance if I wanted to. I could call this code using the following:

int answer;
answer = uncreateable::GetImportantAnswer();

Edit: spelling and grammar

+1
source

Well, actually this program does not compile with g ++:

#include <iostream>
using namespace std;

class Uncopyable{
  public:
    Uncopyable(){}
    ~Uncopyable(){}
  private:
    Uncopyable(const Uncopyable&) {cout<<"in parent copy constructor";}
    Uncopyable& operator=(const Uncopyable&) { cout << "in parent assignment operator";}
};

class Derived: private Uncopyable{

};

int main() {
  Derived a;
  Derived b = a;
}

compiler output:

$ g++ 23183322.cpp 
23183322.cpp:10:88: warning: control reaches end of non-void function [-Wreturn-type]
    Uncopyable& operator=(const Uncopyable&) { cout << "in parent assignment operator";}
                                                                                       ^
23183322.cpp:13:7: error: base class 'Uncopyable' has private copy constructor
class Derived: private Uncopyable{
      ^
23183322.cpp:9:5: note: declared private here
    Uncopyable(const Uncopyable&) {cout<<"in parent copy constructor";}
    ^
23183322.cpp:19:15: note: implicit copy constructor for 'Derived' first required here
  Derived b = a;
              ^
1 warning and 1 error generated.
0
source

Source: https://habr.com/ru/post/1537275/


All Articles