int main() {
class A {
public:
static double test_code(const A& a);
};
class B : public A {
public:
B(int i) { };
static double test_code2(const B& b);
};
A::test_code(2);
B::test_code2(2);
return 0;
}
The first method call will not compile, while the second will. Is there any way to make this conversion work? thanks
No, that cannot be done for an implicit type conversion. There are multiple reasons for that, the simpler is that for that to be an option the compiler would have to know at the place of call about all possible types that extend
Aand then check whether any/all of them can be implicitly converted from anint, resolve potential ambiguities… note that all types extending from a given type is an open set that can be extended after the current translation unit has been built!You can achieve something alike that in different ways, like for example creating a function that takes the
intand returns anAobject, or explicitly creating theB. But none of them allow for an implicit conversion (i.e. without modifying the code to explicitly request the path for the conversion).