top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Explain how static member functions are called in C++?

+1 vote
399 views
Explain how static member functions are called in C++?
posted Feb 3, 2015 by Alwaz

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Static member functions are called by referencing its containing class without using an object.
The following code snippet illustrates the use of static member functions.

Test::set_number(22);
Test::print_number();

where set_number() and print_number() are static functions and they are defined as follows in the class Test:

 static void set_number(int arg) { 
                   si = arg; // si is an int var
         }
         static void print_number() {
             cout << "Value of si = " << si << endl;
         }
answer Feb 4, 2015 by Mohammed Hussain
Similar Questions
+1 vote

I have written a simple program and stored it with .c file,
When i compiled it with g++ it is getting compiled and giving the proper output when i run that,
but when i compile the same using gcc then it is throwing error.

Below is the sample .c file
#include<stdio.h>
struct A{
        private:
        int a;
        public:
        int sum(){
        a=10;
        }
        void print()
        {
        printf("%d\n",a);
        }
        A()
        {
                printf("Constructor\n");
        }
        A(int b)
        {
                printf("Copy Constructor\n");
        }
        ~A()
        {
                printf("Destructor\n");
        }
};
main()
{
        struct A a(10);
        a.sum();
        a.print();
}
0 votes

How does compiler work internally to make sure constant object can access only constant member function ?

...