How to prohibit the use of copy constructor and assignment operator? Those. so that only another class could use the class, and in main it was impossible to create an instance of this class.

In main should not be compiled, but in another class it would be possible:

A a; A a(b); A a=b; 
  • those. so that the instance could not be created in main - Fllw
  • the prohibition of the copy and assignment constructor does not prevent the creation of an instance of the class. As an option, put them in private. - Komdosh
  • it is necessary to do so that only another class could use the class, and in general it was impossible to create at all - Fllw

1 answer 1

You can solve this problem using a friendly class:

 class myClassClose{ myClassClose(); //конструктор ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ myClassClose(const myClassClose&); //конструктор копирования myClassClose(myClassClose&&); //конструктор пСрСмСщСния myClassClose operator = (const myClassClose&) const; //ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ присваивания myClassClose operator = (myClassClose&&) const; //ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ присваивания с ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ΠΌ public: friend class myClass; }; class myClass{ myClassClose a; public: myClass(){} }; 
  • what kind of transference? - Fllw
  • @Fllw He meant the "move operator" - StateItPrimitive
  • @StateItPrimitive, yes, thank you - Komdosh
  • @Fllw new C ++ 11: rvalue links and the semantics of a perfect transfer (guarantee that there is no unnecessary copying). On HabrΓ© can partially penetrate. - D-side