static variables inside a member function

Started by
2 comments, last by CastorX 15 years, 7 months ago
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!
Advertisement
Why do you want for p1p2 variable to be static at all?
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.
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!

This topic is closed to new replies.

Advertisement