covariant return type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

covariant return type

Hi I read about covariant return type in some article. What is this as I never heared of same.

7th Jun 2020, 12:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
11 Answers
+ 3
An interesting topic. I would speak in terms of Java since both languages have a lot in common. While overriding a method, we must have the same return type along with the name and type of arguments in the child class. So this means that we did not give a variant type. But covariant return type allows us to change the return type to sub class type while overriding a method in the child class. A simple example. https://code.sololearn.com/cD24tRFxHJ1q/?ref=app
7th Jun 2020, 12:59 PM
Avinesh
Avinesh - avatar
+ 3
Ketan Lalcheta To answer your first question,covariance is quite a useful concept in the the implementation of things called virtual constructors or factory functions.This is a strong feature in OOP since,given only a pointer to an interface(abstract) base class,you can create new objects of the same type as the one pointed to by this pointer without knowing or even caring about this derived object's type.Hence the name virtual constructors. See the example below which is by far the most common and useful use of this feature Class A{public: virtual A* create()=0;} Class B:public A{public: B* create(){return new B{};} Now given an A pointer(I'll use ptr in this example)which already points to a B object,we can easily create a new B object like this: auto Bptr=ptr->create(); To correct you on the second part of your question,you can,and most probably should,change the return type of that method in class B to a B*,for purposes of clarity.
7th Jun 2020, 5:47 PM
Anthony Maina
Anthony Maina - avatar
+ 1
Answer to 1st part of the question. This is a very simple example of a singleton design pattern where you allow only a single object of your class to be created. So in this case you return an object from a method. There are other use cases including factory design pattern and many more that I cannot think of right now. Here is an example. https://code.sololearn.com/c14h6jNDSvKc/?ref=app
7th Jun 2020, 5:09 PM
Avinesh
Avinesh - avatar
+ 1
On your second question,yes your missing a point.A critical one for that matter. B* p2 = pA->getNewObject(); The type of p2 should be A* not B*.Why? Since the essence of runtime polymorphism is to enable you, the programmer,to manipulate derived class objects using base class pointers. On the third question.First you should be aware that not all classes have copy constructors or assignment operators.In such classes these functions are explicitly deleted to prevent copying.Given an already existing base class pointer you only really have two ways to create a new object of such a type 1.Initialize a new derived class object the normal way( using() or {} ) in which case you have to deal with the type of the object which can be messy at times. 2.Use a factory function,which in my view as a neat and clean way of obtaining a new object.
7th Jun 2020, 6:46 PM
Anthony Maina
Anthony Maina - avatar
0
Thanks Avinesh Hi All , Below is mine trial on covariant return in c++. https://code.sololearn.com/cDAFpI5GwETz/?ref=app Few questions: I never come across to return object from a class method... Because we generally access method using class object. What is the need to return object from a method? At all method return object, I can't change return type of getNewObject method.. both are having return type as A* and I can't change also return type of method in class B from A* to B*
7th Jun 2020, 5:00 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Anthony Maina , for second question i.e. I cannot have return type of method in B class as B* was my wrong opinion. I changed and updated code for same... However, updated code does not work as new object I am returning from B class method cannot be assigned to pointer of class B. If so, what's the point or am I missing something? Also , I understand your point of factory concept to get the other object but why not copy constructor or assignment operator ? I myself never have used a method to get object except factory or singleton... And once object is available, what's big deal to get another object and would surely go for copy constructor... This is something different I might not aware about.. is this have some added advantage? One last point, do we have virtual constructor in c++? I don't think so.. destructor can be virtual not the constructor
7th Jun 2020, 6:01 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Onto your final question.Yes there is such a thing as a virtual constructor in C++.It is not a constructor in a literal sense since it's just but an ordinary member function.However it's appropriately called a virtual constructor since its call is resolved at runtime(hence the word virtual) and results in creation of a new object(which is what contructors typically do).
7th Jun 2020, 6:48 PM
Anthony Maina
Anthony Maina - avatar
0
Interface inheritance is a broad but a relatively easy concept to understand.And as such,I cannot be able to exhaustively cover it in this forum.
7th Jun 2020, 6:50 PM
Anthony Maina
Anthony Maina - avatar
0
The type of p2 should be B not A... Isn't it amazing ? We already had object of B which is added as base pointer i.e. A*....now on it , we are again asking to have object but that also we are getting as base pointer... We only are designing our class structure so rather than deleting copy constructor and getting object like this isn't a overhead of function call ? If new object is required , why to stop copy constructor and put another method ? Sorry if I am stretching... I do understand the importance of polymorphism.. my concern is why to have such covariant methods ? If another object is required, go with copy constructor... What's a big deal in it...?
7th Jun 2020, 7:34 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
And I am still wondering on virtual constructor... We don't have virtual keyword before constructor...is this still true, right?
7th Jun 2020, 7:36 PM
Ketan Lalcheta
Ketan Lalcheta - avatar