my question is that when we use static data member and ststic function and nobody can not satisfy me about this question i need actual reason of using the static data member and ststic member function

Recommended Answers

All 6 Replies

A static data member belongs to the class rather than to an individual instance of the class. Likewise with a static member function. A reasonable way to think about them at a high level is to treat the class as acting like a namespace for those members:

class Foo
{
public:
    static int bar;
    static void baz();
};

...

int main()
{
    cout << Foo::bar << '\n';
    Foo::baz();
}

Comparable in many cases, but not identical is the namespace concept:

namespace Foo
{
    int bar;
    void baz();
}

int main()
{
    cout << Foo::bar << '\n';
    Foo::baz();
}

In both cases you can use the name of the class or namespace as a qualifier to access the member. With classes you can also access static members through an object:

int main()
{
    Foo obj;

    cout << obj.bar << '\n';
    obj.baz();
}

Under the hood, the Foo class can be imagined something like this:

int static_cls_Foo_public_bar;
void static_cls_Foo_public_baz();

class Foo {};

bar and baz are actually closer to global objects that are internally checked for correct visibility and access by the compiler. The objects themselves are not baked into an instance, but still tied closely to the class.

When would you use a static member? Well, whenever you need an object or function that applies to all instances or to the class as a whole. One good example is reference counting. If you're counting the number of instances, then each instance having an independent copy of the counter is ineffective. Using a global variable is dangerous. In this case a static counter does what you want.

Here you go few examples of using static members:

#include <iostream>
using namespace std;

class MyClass{
    static int counter;
public:
    void print(int& i){ cout << "Number of instances: " << counter << endl; }

    MyClass(){ print(++counter); }
    ~MyClass(){ print(--counter); }
};

int MyClass::counter = 0; // Static member initialization

int main(){
    MyClass c1;
    // Bla bla bla bla
    MyClass c2;
    // Some more code
    MyClass* c3 = new MyClass();
    // ...
    delete c3;
    return 0;
}

Output will be:
Number of instances: 1
Number of instances: 2
Number of instances: 3
Number of instances: 2
Number of instances: 1
Number of instances: 0

or something like

#include <iostream>
using namespace std;

class MyClass{
    const int MY_CONST_INT = 10;
public:
    MyClass(){ cout << MY_CONST_INT << endl; }
};



int main(){
    MyClass c1;
    // Bla bla bla bla
    MyClass c2;
    // Some more code
    c1 = c2; // <- ERROR, there is no copy assignment operator
             // created because of const data member.
    return 0;
}

Here we have a problem, to solve it we can create our own copy assignment operator and waste time and time, editing it each time you change something.
Or we can simulate this behaviour using static members.

#include <iostream>
using namespace std;

class MyClass{
    const static int MY_CONST_INT = 10;
public:
    MyClass(){ cout << MY_CONST_INT << endl; }
};



int main(){
    MyClass c1;
    // Bla bla bla bla
    MyClass c2;
    // Some more code
    c1 = c2; // <- Works just fine.
    return 0;
}

Problem solved.
Output will be:
10
10

Here you go, answer to your pm. Code should look simple, if you need any extra explanations, please post them below.

#include <iostream>
#include <string>
using namespace std;

class Runner{
private:

    static int max_distance_;
    static string max_name_;

    int distance_;
    string name_;

public:
    Runner(string name = "Bob"){ name_ = name; cout << "Hi, my name is " << name << endl; }

    void setDistace(const int& i){
        distance_ = i;
        if (i > max_distance_){
            max_distance_ = distance_;
            max_name_ = name_;
        }
    }

    void print(){
        cout << name_ << " has total distance of: " << distance_ << endl;
    }

    static void printTheBest(){
        if (max_distance_ > 0 && max_name_ != "")
            cout << max_name_ << " is the best Runner with total distance of: " << max_distance_ << endl;
    }
};

int Runner::max_distance_ = 0;
string Runner::max_name_ = "";

int main(){
    Runner r1("Jimmy");
    Runner r2("Timmy");
    Runner r3;
    Runner r4("Sasha");

    cout << endl;

    r1.setDistace(550);
    r2.setDistace(800);
    r3.setDistace(650);
    r4.setDistace(700);

    r1.print();
    r2.print();
    r3.print();
    r4.print();

    cout << endl;

    Runner::printTheBest();

    return 0;
}

To my mind, the main reason to use class static variables and methods is to control the namespace, and keep what are effectively global variables from polluting the system root namespace. Decepticon was spon on with his examples, I think. IMO, class statics are more flexible than using specific name-spaces, although in effect they can be very similar.

May I ask, why my post above has -1.
I answered to his request, or should I say, solved his problem, right there...

I answered to his request, or should I say, solved his problem, right there...

I'd wager that's precicely why you got downvoted. Solving homework problems for people without any effort on their part is generally considered to be unhelpful because there's no practical learning involved.

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.