static functions can only access static variables ?

i read static functions can only access static variables and not normal variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
class Dealer
{

    private:
    char mobileno[11];
    char dealername[25];
    char dealeraddress[51];
    char dealercity[25];
    char phoneno[11];
    public:
    static void showID()
    {
        int companyID=6519;
        cout<<"the company id is\t"<<companyID<<endl;
    }
    void get()
    {
        cout<<"enter mobile no:"<<endl;
        cin>>mobileno;
        cout<<"enter dealer name:"<<endl;
        cin>>dealername;
        cout<<"enter dealer address:"<<endl;
        cin>>dealeraddress;
        cout<<"enter dealer city:"<<endl;
        cin>>dealercity;
        cout<<"enter phone no:"<<endl;
        cin>>phoneno;
    }
    void print()
    {
        cout<<"the mobile no is"<<mobileno<<endl;
        cout<<"the dealer name is"<<dealername<<endl;
        cout<<"the dealer address is"<<dealeraddress<<endl;
        cout<<"the dealer city is"<<dealercity<<endl;
        cout<<"the phone no is"<<phoneno<<endl;
    }
};
//int Dealer::companyID=6519;
int main()
{
    Dealer D;
    Dealer::showID();
    D.get();
    D.print();
    return 0;
}


so i wrote this program, i am not able to get how can static void showID a static function access int companyID a normal variable, can someone please explain.
Ok, so you seem to need a more explicit statement: Static methods inside a class can only access static class variables or variables declared inside the static method. The variable companyID is declared inside the static method and is therefore available for use inside this static method.

That should take care of it, I hope.
ok thx then what is the use of static functions ? is the use of static function only being able to access the function without declaring an object like Dealer::showID();
Last edited on
I thinks that their use also has to do with the fact that they are static: they are shared between different instances of this class instead of having each object its own function.

Imagine them as being class specific instead of object specific like most functions
Topic archived. No new replies allowed.