Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 访问静态成员_C - Fatal编程技术网

C 访问静态成员

C 访问静态成员,c,C,我已经在静态方法中声明了一个静态成员。如下所示: static void temp1(param..){ static gint x,y ; #TODO what you needed values get changed here for x,y; } 我想在同一个文件中的其他静态方法中访问这两个 static void temp2 ( param .......){ accessing the x,y

我已经在静态方法中声明了一个静态成员。如下所示:

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

        #TODO what you needed

        values get changed here for x,y;
   }
我想在同一个文件中的其他静态方法中访问这两个

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

我该怎么做。。?我不想声明public member,也不想更改方法param。

这可能就是您想要的:

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和y是全局可访问的,但只能在文件中访问,就像静态过程一样。我认为这是你最接近你想要的东西。

你需要了解以下两件事:

Scope

静态变量的作用域仅在声明它们的函数中
。它们不能在外部访问

但是变量的
生命周期贯穿于整个程序
,也就是说,它们将保留值,直到程序运行为止

因此,您可能希望在函数之外声明变量。所以不是

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

        #TODO what you needed

        values get changed here for x,y;
   }
你可以

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

        values get changed here for x,y;
   }
我认为,如果不更改第二个函数的参数,就不可能使用您所拥有的确切用例。

static int getInnerStatic(int*\ux,int*\uy,int ignore);
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
}
静态空隙温度1(参数..){ 静态int x,y; //////////////// if(getInnerStatic(&x,&y,1)) 返回; //////////////// #做你需要的事 在这里,x,y的值被更改; } 静态int getInnerStatic(int*\ux,int*\uy,int忽略){ 静态int innervoke=0; 静态int x,y; if(innerInvoke==1){ x=*x; y=*y; 返回innerInvoke;//1 } 如果(忽略) 返回innerInvoke;//0 InnerInvoke=1; temp1(/*任何参数…*/); InnerInvoke=0; *_x=x; *_y=y; 返回innerInvoke;//0 } //获取静态x y: 静态空隙温度2(参数……){ int getX,getY;
getInnerStatic(&getX,&getY,0);//以下是您尝试执行的一个示例:

#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;
}
请注意,当您尝试使用作用域解析运算符
访问temp1中的x时,会发生错误。下面是解决此问题的方法

#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;
}
#包括
使用名称空间std;
名称空间临时{
第一节课{
公众:
静态int x,y;
};
int temp1::x=5;
int temp1::y=7;
}
静态空洞temp2(){

不能你不能用你现有的代码来做这件事,你需要修改你的代码,使x和y成为静态实例变量,这样你就可以在所有的静态方法中访问它们。

有了这些限制,我认为这是不可能的。我想看看我是否错了。没有全局变量或函数参数,你真的做不到。这不是无关的例如,levant可能使用Messaging您将生存期与作用域混淆。“x”和“y”是函数temp1()中的局部变量[(作用域限制为temp1()]没有“成员”或“方法”在C中,这不是OP想要的。即使您没有导出x&y变量,它们在文件中仍然是技术上公开的。据我们所知,可能会有另一个x&y全局定义来做其他事情,而这个建议会把事情搞砸。
error: ‘temp1’ is not a class or namespace
#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;
}