What is the use of static functions and static member functions. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

What is the use of static functions and static member functions.

28th Jul 2017, 6:30 AM
Vijay Kumar V H
Vijay Kumar V H - avatar
2 Respostas
+ 1
if you have a class with a static function that function can be called without ever creating an instance of said class. like so: class::func(). A static member function will only be able to access static member variables. You could for example have a timer class that returns the time the Programm has run when calling Timer::getRuntime (). But how is this usefull? Imagine you have your Programm split in 20 .cpp and .h files. whenever you complete a tasks in one of your files you might want to log the time using our timer class. if you function is static all you will have to do is include your timer.h file and call Timer::getTime (). Otherwise you would have to create a Timer object (or get it somehow) in each of your other classes and store it (or get a fresh one everytime).
3rd Aug 2017, 9:37 AM
Fabian Ess
Fabian Ess - avatar
+ 1
still I'm not getting cleared with the topic, can you share some source for this.
3rd Aug 2017, 4:13 PM
Vijay Kumar V H
Vijay Kumar V H - avatar