GeekInterview.com
Series: Subject: Topic:
Question: 38 of 267

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?
Asked by: makan007 | Member Since Nov-2010 | Asked on: Nov 29th, 2010

View all questions by makan007

Showing Answers 1 - 1 of 1 Answers
amitmanish

Answered On : Mar 4th, 2011

View all answers by amitmanish

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.