Item 31. Covariant Return Types & item 32. Preventing Copying

xiaoxiao2021-03-05  27

Item 31. Covariant Return Types

Covariant: Change, follow. (You change me) Usually, an Overriding function must have the same return type with the rewritten function. However, nothing doing, CoVariant Return Types is not required. The following inheritance system: Class Shape {public: // ... Virtual Shape * Clone () const = 0; // prototype //... }; Class Circle: Public Shape {public: Circle * Clone () Const; // Covariant returnipes // ...};

The return type of Shape and Circle's Clone member function is different, but it can run normally. Why? Circle is a subclass of Shape, so Circle * can be automatically converted into shape *. This is CoVariant Return Types.

Item 32. preventing Copying

If you do not declare a COPY constructor, the compiler will automatically give one. Sometimes I don't want the COPY constructor being called, how do I do? 1) Declare one, but private 2) declare one, but not real.

Both of these methods are controlled from the perspective of the compiler, which is used to compile.

转载请注明原文地址:https://www.9cbs.com/read-35492.html

New Post(0)