class Base {
publicvirtualvoid Get() = 0;
virtual ~Base();
};
class Derived1 : Base {
privatebool value;
publicbool Get() {
return value;
}
};
class Derived2 : Base {
privateint value;
publicint Get() {
return value;
}
};
//and then have something like
Base* base = new Derived2();
int temp = base->Get();
I know C++ supports covariant return types in virtual overloaded methods, but is there any way to do this with native types?
Hey there. I have been away from C++ for a while now but I tried your code and unfortunately it turned out to be pretty much pseudocode.
I messed around with it and got it to compile but I had to change it a lot. What you want only seems to work if you place every overloaded method in the vase class and if one of them is a pure virtual method than all overloaded methods in that base class must also be pure virtual. At least this is what I came up with after experimenting. Also, note that the return value is not enough to overload a method. Only the parameters and name of the method make it's signature unique.
class Base {
public:
virtualvoid Get() = 0;
virtualbool Get(int i) = 0;
virtualint Get(float x) = 0;
virtual ~Base() { }
};
class Derived1 : public Base {
bool value;
public:
void Get() { }
bool Get(int i) { }
int Get(float x) { }
};
class Derived2 : public Base {
int value;
public:
void Get() { }
bool Get(int i) { }
int Get(float x) { }
};
int main()
{
Base* base = new Derived2;
//int temp = base->Get(); // I commented this out because you are trying to set a void method to an int
return 0;
}
You can't do that. C++ is a statically typed language. In the last lines of your example, how is the compiler supposed to know what type Get returns?
The important thing to notice is that you even if this was possible (it kind of is, with a bit of work), such thing would be entirely useless.
As per my knowledge for the function overriding the signature of base class and derived class should be same in fact there should not be any difference in their return type
Yes, C++ virtual methods support covariant return type, which means an override method must return etither the return type of the overrided one or a child type of it (it must also be a pointer or reference).
Here neither bool or int is a child type of void so this can't work. An example of covariant methods:
1 2 3 4 5 6 7 8 9 10
class A
{
public:
virtual A* clone() = 0;
}
class B : public A
{
B* clone() override;
}
template<class Type>
class Base {
public :
typedef Type value_type;
Base(){}
virtual ~Base(){}
virtual value_type Get() = 0;
};
template<class Type>
class Derived1 : public Base<Type> {
public :
typedef Type value_type;
virtual ~Derived1(){}
value_type Get() {
return value;
}
private :
value_type value;
};
template<class Type>
class Derived2 : public Base<Type> {
public :
typedef Type value_type;
virtual ~Derived2(){}
value_type Get() {
return value;
}
private :
value_type value;
};
int main () {
//and then have something like
Base<int>* base = new Derived2<int>();
int temp = base->Get();
cout<<temp<<endl;
delete base;
return EXIT_SUCCESS ;
}
class Base {
public:
virtualint Get() = 0;
virtual ~Base();
};
class Derived1 : public Base {
privateint value;
publicint Get() {
return value > 0 ;
}
};
class Derived2 : public Base {
privateint value;
publicint Get() {
return value;
}
};
//and then have something like
Base* base = new Derived2();
int temp = base->Get();