Broad Network


C++ Static Member Function

Some Features of C++ Entities – Part 8

Forward: In this part of the series, I explain the meaning of C++ static member function (method).

By: Chrysanthus Date Published: 8 Sep 2012

Introduction

This is part 8 of my series, Some Features of C++ Entities. In this part of the series, I explain the meaning of C++ static member function (method).

Meaning
A static member function is a function in a class that can be called without instanitation of the class. That is, you do not need an instantiated object in order to use the function. To call the function you type the name of the class; this is followed by the scope resolution operator, and then the name of the function. In the declaration, the static member function is begun with the specifier, static.

Example
In the following program, there is one non-static function and one static function. Read and try the code:

#include <iostream>
using namespace std;

class TheClass
    {
        public:

        void fn()
            {
                cout << "I am not from a static function" << '\n';
            }

        static void fnSt()
            {
                cout << "Hi there, I am from a static function" << '\n';
            }
    };

int main()
    {
        TheClass anObj;
        anObj.fn();

        TheClass::fnSt();

        return 0;
    }

Note how the static member function has been used without the instantiation of the class.

Prototype static Member Function
You can also have a prototype member function that is static. The function definition is then given outside the class description, with the use of the class name and the scope resolution operator. The following program illustrates this:

#include <iostream>
using namespace std;

class TheClass
    {
        public:

        static void fnSt();
    };

void TheClass::fnSt()
            {
                cout << "Hi there, I am from a static function" << '\n';
            }

int main()
    {
        TheClass::fnSt();

        return 0;
    }

There is something to note here. In the function definition outside the class description, the specifier, static, is not used. The return type, void, is used in front of the class name as expected. After the class name you have the scope resolution operator and the name of the function. The parentheses and the rest of the function features follow.

Static Function also Part of Instantiated Object
Just as a static object is part of any instantiated class object, the static member function is also part of any instantiated class object. The value of a static object is the same for all instantiated class objects. So too, the static method (content) is the same for all instantiated class objects. It happens that non-static member functions are also the same for all instantiated class objects. The following example shows the use of the static member function in an inatatiated class object. It is used like normal (non-static) member functions. Whatever, is the situation, the static member function is used (called) without the specifier, static. The specifier, static, is only typed inside the class description during declaration, not during function call outside the class description. It is also not typed when the function is defined outside the class description.

#include <iostream>
using namespace std;

class TheClass
    {
        public:


        void fnSt()
            {
                cout << "Hi there, I am from a static function" << '\n';
            }
    };


int main()
    {
        TheClass anObj;
        anObj.fnSt();

        return 0;
    }

This is the end of the article. And we are at the end of the series; I hope you appreciated it.

I assume you have completed this series. It means you already had the professional skills in C++. Now you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

Chrys

Programming Solutions

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message