Sagar
Sagar

Reputation: 2385

Accessing the Static member

I have declared the one static member inside the static method. like as follws:

   static void temp1(param..){
        static gint x,y ;

        #TODO what you needed

        values get changed here for x,y;
   }

And I want to access this Two in other static method within the same file.

  static void temp2 ( param .......){
         accessing the x,y
  }

How should I do it..? I don't want to declare public member and also don't want to change the method param's .

Upvotes: 2

Views: 95

Answers (5)

Prasad Shinde
Prasad Shinde

Reputation: 662

You cant do that with your existing code you need to modify your code to make x and y as static instance variables so that you can access them in all the static methods.

Upvotes: 0

smac89
smac89

Reputation: 43226

Here is an example of what you are trying to do:

#include <iostream>
using namespace std;

static void temp1() {
    static int x,y ;
    x = 5;
    y = 8;
}

static void temp2 (){
    cout << temp1::x << endl;
}

int main() {
    temp2()
    return 0;
}

Error message

error: ‘temp1’ is not a class or namespace

Note the error that occurs when you try to access x in temp1 by using the scope resolution operator ::. Here is how to solve this

#include <iostream>
using namespace std;

namespace temp {
    class temp1 {
    public:
        static int x,y;
    };
    int temp1::x = 5;
    int temp1::y = 7;
}

static void temp2 (){
    cout << temp::temp1::x << endl;
}

int main() {
    temp2();
    return 0;
}

Note the namespace is not necessary, but I used it to keep related data together

Upvotes: 0

SliceSort
SliceSort

Reputation: 375

static int getInnerStatic(int* _x, int* _y, int ignore);

static void temp1(param..){
    static int x,y ;

    ////////////////
    if (getInnerStatic(&x,&y,1))
        return;
    ////////////////

    #TODO what you needed

    values get changed here for x,y;
}



static int getInnerStatic(int* _x, int* _y, int ignore){
    static int innerInvok = 0;
    static int x, y;

    if (innerInvok == 1){
        x = *_x;
        y = *_y;
        return innerInvok;//1
    }

    if (ignore)
        return innerInvok;//0

    innerInvok = 1;
    temp1(/*anything as param...*/);
    innerInvok = 0;

    *_x = x;
    *_y = y;

    return innerInvok;//0
}

//get static x y :
static void temp2 ( param .......){
    int getX, getY;

    getInnerStatic(&getX, &getY, 0); // <- accessing the x,y
}

Upvotes: 0

gaurav5430
gaurav5430

Reputation: 13917

You need to understand these 2 things:

Scope

and

Lifetime

the scope of your static variables is only inside the function they are declared. they cannot be accessed outside.

but the lifetime of your variables is throughout your program, that is they will retain the values until the program is running.

So maybe you would like to declare your variables outside of your function. so instead of

static void temp1(param..){
        static gint x,y ;

        #TODO what you needed

        values get changed here for x,y;
   }

you can have

 static gint x,y ;
static void temp1(param..){
               #TODO what you needed

        values get changed here for x,y;
   }

The exact use case you have, i think it would not be possible without changing the second function's arguments.

Upvotes: 0

Lencho Reyes
Lencho Reyes

Reputation: 359

This might almost be what you want:

static gint x,y ;

static void temp1(param..){

  /* TODO what you needed */

  values get changed here for x,y;
}

static void temp2 ( param .......){
  /* accessing the x,y */
}

x and y are globally accessible, but only within the file, just like your static procedures. I think this is as close as you can get to what you want.

Upvotes: 1

Related Questions