Overloading a buggy != operator

S

saadsj

Hello,

I'm in a situation where I'm using a library object that happens to
have a bug in one of its operators. This bug was recently introduced
into a new release of their library. Unfortunately, the option of
reverting back to the previous, working version of the library is not
possible. For discussion purposes, the bug is as follows:

namespace System
{
inline bool operator !=( int inLeft, const Variant& inRight )
{
// While this is not the actual bug... it's pretty close.
return false;
}
}

As you can see the "!=" operator always returns false.

So here it goes... I have no clue if this is possible with some C++
magic so please forgive me if the idea sounds fantasy like or down
right stupid. I would like to somehow overload the library's
implementation of this != operator without having to modify their
source. Then I could put my implementation in a "global" header file
that every file in my project already includes.

If anyone has ideas on how this can be done, your help would be greatly
appreciated. Again, if this question is ridiculous please be gentle. :)


Best regards,

Scott
 
V

Victor Bazarov

I'm in a situation where I'm using a library object that happens to
have a bug in one of its operators. This bug was recently introduced
into a new release of their library. Unfortunately, the option of
reverting back to the previous, working version of the library is not
possible. For discussion purposes, the bug is as follows:

namespace System
{
inline bool operator !=( int inLeft, const Variant& inRight )
{
// While this is not the actual bug... it's pretty close.
return false;
}
}

As you can see the "!=" operator always returns false.

So here it goes... I have no clue if this is possible with some C++
magic so please forgive me if the idea sounds fantasy like or down
right stupid. I would like to somehow overload the library's
implementation of this != operator without having to modify their
source. Then I could put my implementation in a "global" header file
that every file in my project already includes.

If anyone has ideas on how this can be done, your help would be
greatly appreciated. Again, if this question is ridiculous please be
gentle. :)

I am not sure what the problem is. You just described the solution,
implement your own and put it in your header that is included by every
TU that needs it. So, have you already done that and there is some
problem? What kind? If you haven't yet done what you thought about,
why? And how can we help you with what you already know?

V
 
S

saadsj

Sorry Victor, I totally forgot to mention that part. I get an error
that the "body has already been defined for the function". I figured
that because of this error, overloading a function in a pre-existing
namespace was not possible.

Does the error I'm getting seem correct to you, given the situation?

Scott
 
M

Michiel.Salters

Hello,

I'm in a situation where I'm using a library object that happens to
have a bug in one of its operators. This bug was recently introduced
into a new release of their library. For discussion purposes, the bug
is as follows:

namespace System
{
inline bool operator !=( int inLeft, const Variant& inRight )
{
// While this is not the actual bug... it's pretty close.
return false;
}
}

As you can see the "!=" operator always returns false.

I would like to somehow overload the library's
implementation of this != operator without having to modify their
source. Then I could put my implementation in a "global" header file
that every file in my project already includes.

Overloading means adding a function with the same "name" (in this
case "operator !=") but other arguments. That won't help you here.

What you need to do is subclass from System::Variant, and define that
as BugFix::Variant. All functions except operator!= will forward
directly;
you can provide your own !=

However, whatever you do won't fix the use of the original operator!=
in the library itself. And since it's likely inlined, you can't fix it
with
linker tricks. Really, there's a reason people tell you to go back or
forward to a proper version.

Regards,
Michiel Salters
 
S

saadsj

Overloading means adding a function with the same "name" (in this
case "operator !=") but other arguments. That won't help you here.

What you need to do is subclass from System::Variant, and define that
as BugFix::Variant. All functions except operator!= will forward
directly;
you can provide your own !=

However, whatever you do won't fix the use of the original operator!=
in the library itself. And since it's likely inlined, you can't fix it
with
linker tricks. Really, there's a reason people tell you to go back or
forward to a proper version.

Regards,
Michiel Salters

Michiel,

Thank you for the reply. I like your proposed solution, and I think
that works fine for the Variant::eek:perator != (this is where the Variant
shows up on the left side of the comparison).

However, what about when the Variant shows up on the right side of the
comparison? The example I gave in the original posting is the !=
operator for when an integer is on the left and the Variant is on the
right. This function is still defined within the System namespace, but
it is not part of the Variant object... it's kind of a standalone
function.

Again, your help is greatly appreciated.

Best regards,

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top