P
PengYu.UT
Hi,
Are there any walkaround to enable functions in the derived class with
the same function name but different return type?
In the following example, D1 and D2 are B's derived class. I want both
D1 and D2 have the function "doit". But there return type should be
different. Is it possible?
Thanks,
Peng
#include <iostream>
class B {
public:
B() { }
virtual ~B() { }
virtual int doit() const = 0;
};
class D1 : public B {
public:
D1() { }
int doit() const {
return 42;
}
};
class D2 : public B {
public:
D2() { }
virtual double doit() const {
return -42;
}
};
int main() {
B* b = new D1;
std::cout << b->doit() << std::endl;
}
Are there any walkaround to enable functions in the derived class with
the same function name but different return type?
In the following example, D1 and D2 are B's derived class. I want both
D1 and D2 have the function "doit". But there return type should be
different. Is it possible?
Thanks,
Peng
#include <iostream>
class B {
public:
B() { }
virtual ~B() { }
virtual int doit() const = 0;
};
class D1 : public B {
public:
D1() { }
int doit() const {
return 42;
}
};
class D2 : public B {
public:
D2() { }
virtual double doit() const {
return -42;
}
};
int main() {
B* b = new D1;
std::cout << b->doit() << std::endl;
}