当全局变量不合适时保持计数

C++ Keeping count when global variable is not appropriate

本文关键字:全局变量 不合适      更新时间:2023-10-16

我需要协调&发出唯一的连续数字标识符(1,2,3…)

我正在使用微软编译器创建一个c++ DLL,其中所有变量都封装在对象内;在这种情况下,全局变量不是一个选项,实际上在这个项目中被设计掉了。

我已经尝试了以下方法来解决这个问题,并发现通过前向声明访问另一个对象的成员函数是不可能的(N.B.代码来说明我要实现的目标,由于不正确地使用前向声明访问成员函数,它将无法编译):

祖父母类:

//GrandParent
class GrandParent
{
public GrandParent(){}
int addParent()
{
Parent *par = new Parent(this);
return 0;
}
int incrementGrandChildIDNumber()
{
return grandChildIDNumber +=1;//increment
}
private:
int grandChildIdNumber;//keep count.  This NEEDS to be encapsulated. Cannot be a
 //global variable as there will be multiple instances of GrandParent each counting
 //and labeling it's own grand children.
};
父类

:

//Parent
class GrandParent;//forward declaration
class Parent
{
public Parent(GrandParent *ptrToGrandParent): ptr2GP(ptrToGrandParent){}
addGrandChild()
{
id = ptr2GP->incrementGrandChildIDNumber();//but forward declaration does not
    //give access to GrandParent member functions, right??
GrandChild grndChld = new GrandChild(id);
return 0;
}
private:
int id;
GrandParent *ptr2GP;
};

孙子类:

//GrandChild
 class GrandChild
{
 public:
    GrandChild(const int &id):idNumber(id){}
 private:
int idNumber;
};

我已经简化了这个问题,实际上每个类都要长得多,并且在自己的头文件中定义。

我的问题是:如果前向声明不起作用,全局变量在这个项目中不合适,还有什么其他选项可以协调向孙子对象发放ID号?

在不同的文件中单独定义类和声明类,并使用静态变量:

// GrandParent.hh
#ifndef _GrandParent_
#define _GrandParent_
class Parent;
class GrandParent
{
  public:
    GrandParent();
    Parent * addParent();
    int incrementGrandChildIDNumber();
  private:
    static int grandChildIdNumber;
};
#endif

// GrandParent.cpp
#include "GrandParent.hh"
#include "Parent.hh"
int GrandParent::grandChildIdNumber = 0;
GrandParent::GrandParent()
{
}
Parent * GrandParent::addParent()
{
  Parent * par = new Parent(this);
  return par;
}
int GrandParent::incrementGrandChildIDNumber()
{
  return grandChildIdNumber++;
}
// Parent.hh
#ifndef _Parent_hh_
#define _Parent_hh_
class GrandParent;//forward declaration
class GrandChild;
class Parent
{
  public:
    Parent(GrandParent *ptrToGrandParent);
    GrandChild * addGrandChild();
  private:
    int id;
    GrandParent * ptr2GP;
};
#endif
// Parent.cpp
#include "Parent.hh"
#include "GrandParent.hh"
#include "GrandChild.hh"
Parent::Parent(GrandParent * ptrToGrandParent) : 
  ptr2GP(ptrToGrandParent)
{
}
GrandChild * Parent::addGrandChild()
{
  id = ptr2GP->incrementGrandChildIDNumber();
  GrandChild * grndChld = new GrandChild(id);
  return grndChld;
}

// GrandChild.hh
#ifndef _GrandChild_hh_
#define _GrandChild_hh_
class GrandChild
{
  public:
    GrandChild(int id);
  private:
    int idNumber;
};
#endif
// GrandChild.cpp
#include "GrandChild.hh"
GrandChild::GrandChild(int id) : 
  idNumber(id)
{
}

并在main:

中使用
#include "GrandParent.hh"
#include "Parent.hh"
#include "GrandChild.hh"
int main(int argc, char ** argv)
{
  GrandParent grandPa;
  Parent * parent1 = grandPa.addParent();
  Parent * parent2 = grandPa.addParent();
  GrandChild * gChild1_1 = parent1->addGrandChild();
  GrandChild * gChild1_2 = parent1->addGrandChild();
  GrandChild * gChild2_1 = parent2->addGrandChild();
  GrandChild * gChild2_2 = parent2->addGrandChild();
}

有一个函数生成唯一标识符的单例类。(这可以是基于计数器的顺序,也可以是基于您喜欢的任何其他方案。)

或者,这可以只是基类中的一个函数,它返回一个基于静态计数器的唯一id。您甚至可以将其放在基类构造函数中。