I have some classes, each of them has a static method which I use to create them (I wan't my classes to be created only with new, don't ask why):
1 2 3 4 5 6 7 8
// Base class
public:
static XGE_GUIElem* create(int x = 0, int y = 0, int w = 0, int h = 0);
// Extends base class
public:
static XGE_Stage* create(int x = 0, int y = 0);
private/protected:
using XGE_GUIElem::create;
And Visual C++ compiler complains about call to ambiguous overload when I create a XGE_Stage, even though the parent one is private (or protected, I don't think this is the problem...).
Is there a way I can get it to work?
Line 3,6: both the base class and the derived class version of create have all the parameters defaulted. If you call create with no arguments, the compiler does not know if you mean to call the base class version, or the derived version. Hence, the ambiguous overload error.
Change one of the functions so that at least one of the arguments is required.
// Base class
class XGE_GUIElem
{
public:
static XGE_GUIElem* create(int x = 0, int y = 0, int w = 0, int h = 0) { returnnew XGE_GUIElem; }
};
// Extends base class
class XGE_Stage : public XGE_GUIElem
{
public:
static XGE_Stage* create(int x = 0, int y = 0) { returnnew XGE_Stage; }
};
int main()
{
XGE_Stage::create();
return 0;
}
Now that I read your question again I see you mention "ambiguous overload" which implies that both versions of create are in the same class. This is nothing to do with inheritance and overridiing functions. You can't have two versions of create like that in the same class - just use different names depending on their return type.
@naraku9333 Thanks, This worked! If I undestood correctly what happened, basically:
- if I just create a function with the same name, I'm "overwriting";
- if I add using statement, I'm overloading parent ones, right?