Static Constant Function

Consider a static const function y() that is private member function of a class Y. What do the static and const mean? When could this function be used?

Questions by makan007

Showing Answers 1 - 3 of 3 Answers

amitmanish

  • Mar 4th, 2011
 

A static function which is private to a class will lead to a compilation error as it will be inaccessible.

If put in the public section it will compile.

Now for const, a const function is meant not to modify any member variables of the object it is accessed for. But since it is static in this case, it won't have any objects associated with it, so there is no meaning of const for static member function. though it can b placed there without generating an error

#include<iostream.h>
class A
{
    static int a;
    //int b;
  public:
    static const void foo()
    {
        a++;
        //b = 10; // this gives compilation error "member b cannot be used without an object"
    }
    static void func()
    {
        cout<<a;
    }
};
int A::a=0;
void main()
{
    A::foo();
    A::func();
}

this prints 1 as output.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions