Static help!

I want to overload a function that has arguments of:

void TimedPrint(std::string name, TYPE x, sf::Time printSpeed)

so I can use other data types like vectors. But to do this I need each function call to have its own sf::Clock (basically an object that counts and returns a sf::Time).

Example:

1
2
3
4
5
6
7
8
9
10
void TimedPrint(std::string name, float x, sf::Time printTime)
{
	static sf::Clock clock;

	if (clock.getElapsedTime() > printTime)
	{
		std::cout << name << ": " << x << std::endl;
		clock.restart();
	}
}


example calling:
1
2
TimedPrint("Player Pos", player.getpos() /*sf::Vector2f*/, sf::seconds(1));
TimedPrint("score", score /*float*/, sf::seconds(.5));



But this doesn't work when I try to call the same function twice seeing the static is only made once. Will I have to make a vector of objects that do what I want to do, or is there any way I can use only functions?
Last edited on
> I need each function call to have its own sf::Clock
You can achieve this by creating a class with private: sf::Clock and member function void TimePrint(...)

Then, you can be sure that each object has its own clock
Would I have to make a different object type for each type I want to print?
Expanding on what shadowCODE suggested, you can create a class and provide overloads for each of the types you want to print.
1
2
3
4
5
6
7
8
9
10
11
class Foo
{   sf::Clock clock;
public:
    void TimedPrint (string, sf::Vector 2f, sf:Time); 
    void TimedPrint (string, float sf::Time); 
};

    Foo tp1, tp2;

    tp1.TimedPrint ("Player Pos", player.getpos() /*sf::Vector2f*/, sf::seconds(1));
    tp2.TimedPrint ("score", score /*float*/, sf::seconds(.5));  


I left the arguments as you had them, however, I would seriously consider passing name and time values in Foo's constructor and changing TimedPrint to accept just the single typed argument.

1
2
3
  Foo tp1 ("Player Pos", sf:;seconds(1);

  tp1.TimedPrint (player.getpos());
Topic archived. No new replies allowed.