Sign in to follow this  
CastorX

static variables inside a member function

Recommended Posts

I want to boost up my "PointPointDistance" function (and all the others) with static variables. This function is a member function of a class. How can I create a variable inside this function if I want it to be static BUT only inside a created object. I mean, if I try this: float U3DGeometry::UDistanceTester::PointPoint(const UPoint &p1, const UPoint &p2) const { static int call = 0; call ++; static UVector p1p2(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z); return p1p2.Length(); } and then: U3DGeometry::UDistanceTester a,b; a.PointPoint(p,UPoint(0,0,0)); // Inside this function call the call variable is 0 a.PointPoint(p,UPoint(0,0,0)); // Inside this function call the call variable is 1 b.PointPoint(p,UPoint(1,1,1)); // Inside this function call the call variable is 2 <- I want this to be 0 because this is object "b" a.PointPoint(p,UPoint(0,0,0)); // Inside this function call the call variable is 3 I make this whole thing because I want to create DistanceTester objects for every thread in my program (for those are using it). This would make it thread-safe and fast... I hope :) Thank you for your help!

Share this post


Link to post
Share on other sites
Hey, I don't think you can do it with static variables.. the point of static is to have the same value across all objects by defintition.

How about just a private member variable inside your class? Increment it each time PointPoint is called. The scope becomes wider than you need but i'm sure you can live with that.

Share this post


Link to post
Share on other sites
OK. Thank you! I'll move the variables inside the class scope.
I wanted to do this to make the function faster. I made some testes and turned out that these small functions run much faster with static variables. I need it for a real-time application... so all these must be fast!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this