当前位置: 代码迷 >> 综合 >> 全局结构体变量
  详细解决方案

全局结构体变量

热度:36   发布时间:2024-01-21 21:36:30.0

1.在.h头文件中定义struct:  
  struct   JCB   {   //   定义作业控制块JCB      
  char   name[10]; //作业名  
  char   *state;   //作业状态  
  int   time; //估计运行时间  
  int   Wtime; //等待时间  
  int   arrive; //到达时间  
  int   Ftime; //完成时间  
  int   Btime; //开始时间  
  float   Rp; //响应比  
  }*p;  
  struct   SqList{  
  struct   JCB   r[MAXSIZE+1];  
  int   length;  
  };  
  2.在某个.cpp文件中实现变量申明:  
  struct   SqList   L1,L2,L3,L4;  
  3.在需要使用的地方定义(一般放在.h公用模块):  
  extern   struct   SqList   L1,L2,L3,L4;  
  4.这样就可以作为全局变量直接使用了.  

另一种调用共享的结构体方法:

#include <iostream.h>

typedef struct
{
 int c;
}test;
class A
{
 public:
 static test t ;
 };
test A::t={1};
class B
{
 
 public:
 A a;
 void m_fun()
 {
  cout<<a.t.c<<endl;

  }
 
 };
void main()
{

 B b;
 b.m_fun();
   
}

  相关解决方案