Skip to main content
gamedev.net
🔒 Locked

Class Function Overloading Problem

Started by ms291052 3 replies 300+ views
Original Post
ms291052
ms291052
Hello, I''m having a problem with overloading. I have a class like this
class Foo
{
void Bar(MyClass* a);
void Bar(DWORD a);
};
All runs fine and dandy, right? Until I get to the point where I call Bar(0), it gives me an ambiguous error. I want to direct that call to the DWORD version, but it doesn''t work. I''ve found I can hack through the problem by calling it like Bar((DWORD)0); but that''s just that: a hack. Is there any decent way of doing this?
SiCrane
SiCrane
You can add an extra layer of indirection to your function parameter. Like:

class MyClass;

class MyClassPtr {
public:
MyClassPtr(MyClass * ptr) : ptr_(ptr) {}
MyClass * ptr_;
};

class Foo {
public:
void Bar(MyClassPtr a);
void Bar(DWORD a);
};

int main() {
Foo f;
MyClass * ptr;
f.Bar(0);
f.Bar(ptr);

return 0;
}

It''s ugly, but it keeps the ugly localized.
ms291052
ms291052
Hmmm... Thanks for your response. Personally, however, I think that just casting to DWORD''s prettier. Thanks a lot anyway!
JohnBolton
JohnBolton
quote:
Original post by ms291052
... I''ve found I can hack through the problem by calling it like Bar((DWORD)0); but that''s just that: a hack. Is there any decent way of doing this?


Perhaps you could use a reference instead of a pointer. Other than that, "(DWORD)0" is the simplest and most straightforward.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
fallenang3l
fallenang3l
You just violated a good design principle: never overload on a numeric type and a pointer (read up Effective C++). Either scrap this design, or hack it with a cast.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.