what is static function in c++?
when should a function is made static?

c++ has static functions just like the ones on C language, and it also has static class methods, which one do you mean?

Static Functions
Static functions are visible to only the *.cpp file in which it is defined. This is only really useful in programs that have two or more *.cpp files in the project. A static function in one *.cpp file can not be called from functions in other *.cpp files. Static functions are probably most useful in libraries and DLLs where you don't want application programs calling them.

Static Methods

A static class method is one in which there is one, and only one instance regardless of the number of instances of the class itself.  A static method can not access any of the instance variables of the class or call any of the instance class methods.  This means that if a class has two methods, foo() and bar(), and foo() is a static method and bar() is not, then foo() can not call bar(), but bar() can call foo().





class MyClass
{
public:
    static int foo() {return 0;}
    int bar() { return foo(); }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.