C++ 如何在c++;
c++(不是c++11)C++ 如何在c++;,c++,uniqueidentifier,C++,Uniqueidentifier,c++(不是c++11) 假设我的项目中有100个.cpp文件,它们目前正在做一些工作。 所有这些文件目前都包含一些globals.h文件,我可以轻松编辑这些文件 我希望这些文件中的每一个都有它自己的某个对象的实例,我还希望该实例有它被实例化的文件的某个唯一ID。 此外,我希望这些对象由工厂方法创建,并且我需要实例有某种方式供用户处理——这意味着它们不能是匿名的 简言之,我需要一种为我的项目中的所有文件生成唯一ID的方法,我需要该文件能够在所有文件中使用相同的名称访问其自己的ID,但也能够在外部
假设我的项目中有100个.cpp文件,它们目前正在做一些工作。 所有这些文件目前都包含一些globals.h文件,我可以轻松编辑这些文件 我希望这些文件中的每一个都有它自己的某个对象的实例,我还希望该实例有它被实例化的文件的某个唯一ID。 此外,我希望这些对象由工厂方法创建,并且我需要实例有某种方式供用户处理——这意味着它们不能是匿名的 简言之,我需要一种为我的项目中的所有文件生成唯一ID的方法,我需要该文件能够在所有文件中使用相同的名称访问其自己的ID,但也能够在外部访问另一个“管理器”文件中的文件ID 以下是不起作用的选项: 一,。 枚举: 如果我使用一个枚举并给每个文件一个枚举ID,现在我不能在globals中这样做。h:
static thePrivateInstanceInThisFile = theFactory.makeInstance(fileID);
class FileIDGiver{
private:
static int currentID;//initialize to 0 in cpp
int myID;
public:
FileIDGiver(){
myID = currentID++;
}
int getFileID(){
return myID;
}
}
static FileIDGiver theFileId;
static thePrivateInstanceInThisFile = theFactory.makeInstance(theFileId.getFileID());
因为我在每个文件中都需要一个不同的fileID
,它是静态定义的,并且使用我的枚举唯一地命名
二,。
计算自身实例的类
在globals.h中定义:
static thePrivateInstanceInThisFile = theFactory.makeInstance(fileID);
class FileIDGiver{
private:
static int currentID;//initialize to 0 in cpp
int myID;
public:
FileIDGiver(){
myID = currentID++;
}
int getFileID(){
return myID;
}
}
static FileIDGiver theFileId;
static thePrivateInstanceInThisFile = theFactory.makeInstance(theFileId.getFileID());
这将为每个静态文件instace提供一个ID,该ID对于文件来说是唯一的,但现在它在文件外部是不可管理的
我想做一些类似的事情
globals.cpp
int file1ID;
int file2ID;
...
globals.h
extern file1ID;
extern file2ID;
...
file1.cpp
file1ID = theFileId.getFileID();
file2.cpp
file2ID = theFileId.getFileID();
...
当用户需要管理文件时,他会使用文件的ID变量,或者以上述方式创建一个新的变量
这将允许我从外部访问每个唯一且自动的文件ID。
这方面唯一的问题是行file1ID=theFileId.getFileID()代码>仅在运行时执行,在static theprivateinnstanceinthisfile=theFactory.makeInstance(theFileId.getFileID())行之后代码>。
它在编译时执行
我想不出一个好办法来扭转这个顺序,或者做一个完全不同的机械师
再一次——我需要:
自动创建的文件ID
唯一的文件ID(最好是数字)
在所有文件中使用相同变量名的ID(自动,使用globals.h文件中的静态变量定义)
能够使用另一个手动定义的变量手动访问特定的文件ID
请提供一些好的方法来实现这一点
谢谢。这听起来像是一个糟糕的案例
下面是一个解决方案,它为每个文件唯一地分配整数id,然后通过使用文件id调用工厂函数生成唯一的实例
,同时确保实例
工厂在首次使用前已初始化:
idgiver.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
idgiver.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
factory.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
factory.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
globals.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
这听起来像是一个糟糕的案例
下面是一个解决方案,它为每个文件唯一地分配整数id,然后通过使用文件id调用工厂函数生成唯一的实例
,同时确保实例
工厂在首次使用前已初始化:
idgiver.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
idgiver.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
factory.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
factory.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
globals.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
这听起来像是一个糟糕的案例
下面是一个解决方案,它为每个文件唯一地分配整数id,然后通过使用文件id调用工厂函数生成唯一的实例
,同时确保实例
工厂在首次使用前已初始化:
idgiver.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
idgiver.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
factory.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
factory.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
globals.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
这听起来像是一个糟糕的案例
下面是一个解决方案,它为每个文件唯一地分配整数id,然后通过使用文件id调用工厂函数生成唯一的实例
,同时确保实例
工厂在首次使用前已初始化:
idgiver.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
idgiver.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
factory.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
factory.cpp
:
#include "idgiver.h"
IdGiver &getTheIdGiver()
{
static IdGiver theIdGiver;
return theIdGiver;
}
#include "factory.h"
Factory &getTheFactory()
{
static Factory theFactory;
return theFactory;
}
globals.h
:
class IdGiver
{
int id;
public:
IdGiver() : id(0) {}
int getId() {return id++;}
};
IdGiver &getTheIdGiver();
class Instance
{
// ...
};
class Factory
{
// ...
public:
Factory() : {/*...*/}
Instance getInstance(int id) {/*...*/}
};
Factory &getTheFactory();
#include "idgiver.h"
#include "factory.h"
static int thisFileId = getTheIdGiver().getId();
static Instance thisFileInstance = getTheFactory().getInstance(thisFileId);
您可以修改构建过程(例如,Makefile
)来定义一些独特的东西。例如,您可以使用类似的工具编译foo23.cpp
文件(假设在某些Linux系统上;根据您的编译器、操作系统和构建器进行调整)
您可以使用一些shell脚本或任何东西(例如,Makefile
中的特殊规则)获取UNIQUEID
的23。详细信息取决于您的文件命名约定
然后在C或C++代码中适当使用<代码> BaseNe> <代码> >代码> UNIQUID</代码>(可能是脏代码< >如果UnQuid==23 预处理器技巧…)
因此,想法是在构建系统中生成唯一ID
,并通过一些预处理器符号传递它。详细信息是操作系统、编译器、构建系统特定的
你也可以通过生成一些C或C++源文件或头文件(也许使用一些<代码> AWK< /COD>脚本或一些或预处理)在你的构建过程中做一些粗略的操作。
< P>你可以修改你的构建过程(例如你的<代码> MaMeCuff< <代码>)来定义一些独特的东西。例如,您可以使用类似的工具编译foo23.cpp
文件(假设在某些Linux系统上;根据您的编译器、操作系统和构建器进行调整)
您可以使用一些shell脚本或任何东西(例如,Makefile
中的特殊规则)获取UNIQUEID
的23。详细信息取决于您的文件命名约定
然后在C或C++代码中适当使用<代码> BaseNe> <代码> >代码> UNIQUID</代码>(可能是脏代码< >如果UnQuid==23 预处理器技巧…)
所以这个想法是