Is it feasible to have an inherited class to implement an online function having a different return type (not utilizing a template as return)?


Yes. The return types are permitted to become different as lengthy because they are covariant. The C++ standard describes it such as this (ยง10.3/5):

The return kind of an overriding function will be either just like the return kind of the overridden function or covariant using the classes from the functions. If your function D::f overrides a function B::f, the return kind of the functions are covariant when the fulfill the following criteria:

  • both of them are pointers to classes or references to classes98)
  • the category within the return kind of B::f is identical class because the class within the return kind of D::f or, is definitely an unambiguous direct or indirect base class from the class within the return kind of D::f and it is available in D
  • both pointers or references have a similar cv-qualification and also the class enter in the return kind of D::f has got the same cv-qualification as or less cv-qualification compared to class enter in the return kind of B::f.

Footnote 98 highlights that "multi-level pointers to classes or references to multi-level pointers to courses are not permitted."

In a nutshell, if D is really a subtype of B, then your return kind of the function in D must be a subtype from the return kind of the function in B. The most typical example happens when the return types are themselves according to D and B, however they needn't be. Think about this, where we two separate type hierarchies:

struct Base 

struct Derived: public Base 

struct B 

  virtual ~B() 

struct D: public B 

int primary() remove base

  remove b

The main reason this works happens because any caller of func is expecting a Base pointer. Any Base pointer is going to do. So, if D::func offers to always return a Derived pointer, then it'll always fulfill the contract organized through the ancestor class because any Derived pointer could be unconditionally transformed into a Base pointer. Thus, phone callers will invariably get the things they expect.

Additionally to permitting the return type to alter, some languages permit the parameter types from the overriding function to alter, too. Once they do this, they often have to be contravariant. That's, if B::f accepts a Derived*, then D::f could be permitted to simply accept a Base*. Descendants are permitted to become looser with what they'll accept, and more stringent with what they return. C++ doesn't allow parameter-type contravariance. Should you alter the parameter types, C++ views it a completely new function, which means you get into over-loading and hiding. For additional about this subject, see Covariance and contravariance (computer science) in Wikipedia.