Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloading a "library"

Reply
Thread Tools

Overloading a "library"

 
 
Xavier Decoret
Guest
Posts: n/a
 
      06-30-2003
This is a bit *nix related but it still should be suitable for that
newsgroup.

I would like to "overload" a function that is defined in a library. Here
is a dummy example. Let's say I want to find out in my program how many
time the sqrtf() function of libm.a is called.

I would like to define:

unsigned int nbCalls = 0;

inline float
my_sqrtf(const float f)
{
++nbCalls;
return sqrtf(f);
}

Using the my_ naming scheme forces me to change my program's code
(probably using a PREFIX macro that can be set to my_ or to nothing to
be able to compile both versions). But then I have to recompile, which
is a burden because I might have many libraries dependency using the
sqrtf function.

An approach would be to create a my_libm.a where I redefine my version
of sqrtf() and link with that lib instead of libm.a.

The problem is then: how can I, in this library, call the original
sqrtf() function? I cannot link it with libm.a because I would get a
multiple defined symbol problem, right?

I was wondering if anyone on this newsgroup have been tampering with
this. If yes, any hint would be greatly appreciated!


--
+-------------------------------------------------+
| Xavier Décoret - Post Doct |
| Graphics Lab (LCS) - MIT |
| mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      06-30-2003
In article <bdpgd1$2en3$(E-Mail Removed)>,
(E-Mail Removed) says...

[ ... ]

> I would like to "overload" a function that is defined in a library. Here
> is a dummy example. Let's say I want to find out in my program how many
> time the sqrtf() function of libm.a is called.


I'd use namespaces:

namespace instrumented {
float sqrtf(float arg) {
++nbCalls;
return std::sqrt(arg);
}
};

In serious code, I'd generally advise against using most namespace names
directly. If you use a namespace alias instead, this sort of change
becomes trivial. e.g. if you started with:

float x = std::sqrtf(2.0f);

It would be fairly difficult to change things. OTOH, if you started
with:

namespace lib = std;

float x = lib::sqrtf(2.0f);

Then changing to a totally different set of functions becomes trivial:

namespace lib = instrumented;

and the remaining code stays the same.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments