本文介绍了C ++全局外部常量定义在运行时可用跨多个源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在运行时定义的整数常量。此常量需要在全局和跨多个源文件可用。我目前有以下简化情况:

I have an integer constant that is to be defined at runtime. This constant needs to be available globally and across multiple source files. I currently have the following simplified situation:


  • ClassA.h 声明 extern const int someConstant;

<$ c $

ClassA.cpp uses someConstant at some point.

Constants.h 声明 extern const int someConstant;

main.cpp 包括 ClassA.h Constants.h ,声明 const int someConstant ,并且在 main()期间尝试在运行时将 someConstant 初始化为实际值。 p>

main.cpp includes ClassA.h and Constants.h, declares const int someConstant, and at some point during main() tries to initialize someConstant to the real value during runtime.

这使用了 char * 在所有文件中全局可用的程序的名称,它的声明和定义完全一样,我想在这里声明和定义,但我不能让它使用 int

This works flawlessly with a char * constant that I use to have the name of the program globally available across all files, and it's declared and defined exactly like the one I'm trying to declare and define here but I can't get it to work with an int.

我首先遇到错误:未初始化的const'someConstant'[-fpermissive] 它在 main.cpp ,后来我得到一个错误:赋值只读变量'someConstant'我假设是因为 someConstant 正在得到默认初始化开始。

I get first an error: uninitialized const ‘someConstant’ [-fpermissive] at the line I'm declaring it in main.cpp, and later on I get an error: assignment of read-only variable ‘someConstant’ which I presume is because someConstant is getting default initialized to begin with.

有办法做什么我' m想实现这里?提前感谢!

Is there a way to do what I'm trying to achieve here? Thanks in advance!

EDIT (根据 @WhozCraig ):相信我:它是不变的。我不发布MCVE的原因是由于三个原因:这是一个作业,源是西班牙语,因为我真的想保持这个问题作为一般(和可重复使用)尽可能。我开始写这个例子,中间它使我不是最清晰的问题。我会再次解释。

EDIT (per request from @WhozCraig): Believe me: it is constant. The reason I'm not posting MCVE is because of three reasons: this is an assignment, the source is in Spanish, and because I really wanted to keep the question as general (and reusable) as possible. I started out writing the example and midway it striked me as not the clearest question. I'll try to explain again.

我被要求创建一个程序,创建一个进程,反过来生成两个孩子(那些依次会产生两个更多,等等)。该程序将单个参数作为它将产生的代数。基本上创建一个进程的二叉树。每个过程都必须提供关于他自己,他的父母,与原始过程的关系,以及他的孩子(如果有的话)的信息。

I'm asked to build a program that creates a process that in turn spawns two children (those in turn will spawn two more each, and so on). The program takes as single argument the number of generations it will have to spawn. Essentially creating sort of a binary tree of processes. Each process has to provide information about himself, his parent, the relationship with the original process, and his children (if any).

strong> ClassA 实际上是一个包含有关进程(PID,PPID,子进程PID,与原始进程的关系程度等)信息的类。对于每个 fork 我创建这个类的一个新实例,所以我可以保存这些信息,并在屏幕上打印。

So, in the example above, ClassA is really a class containing information about the process (PID, PPID, children's PIDs, degree of relation with the original process, etc). For each fork I create a new instance of this class, so I can "save" this information and print it on screen.

当我定义与原始进程的关系时,有一个点,我需要知道调用程序时使用的参数,以检查这个进程是否没有孩子(改变那个特定进程的输出)。这是我需要从主体的常数:要产生的代数,树的深度。

When I'm defining the relationship with the original process, there's a single point in which I need to know the argument used when calling the program to check if this process has no children (to change the output of that particular process). That's the constant I need from main: the number of generations to be spawned, the "deepness" of the tree.

编辑2 :我必须道歉,这是一个漫长的一天,我不直接思考。我将源代码从C切换到C ++只是为了使用一些OO功能,完全忘记了 OO范例内的 。我只是意识到,当我解释这个,我可能解决这个与我的类中的静态/类变量(用原始进程初始化),它可能不是常量(虽然语义上是),但它应该工作,对不对?此外,我还意识到我可以只是初始化上一代的孩子与一些不可能的PID值,并使用它来检查它是否是最后一代。

EDIT 2: I'll have to apologize, it's been a long day and I wasn't thinking straight. I switched the sources from C to C++ just to use some OO features and completely forgot to think inside of the OO paradigm. I just realized while I was explaining this that I might solve this with a static/class variable inside my class (initialized with the original process), it might not be constant (although semantically it is) but it should work, right? Moreover I also realized I could just initialize the children of the last generation with some impossible PID value and use that to check if it is the last generation.

对不起,谢谢你的帮助:似乎问题是有效的,但它是一个错误的问题一直问。

Sorry guys and thank you for your help: it seems the question was valid but it was the wrong question to ask all along. New mantra: walk off the computer and relax.

但是,只是为了简要介绍和坚持下去,绝对不可能创建一个将在运行时定义的全局常量

But just to recap and to stay on point, it is absolutely impossible to create a global constant that would be defined at runtime in C++, like @Jerry101 says?

推荐答案

在C / C ++中,const定义为编译时间。

In C/C++, a const is defined at compile time. It cannot be set at runtime.

在运行时可以设置 const char * xyz; 声明一个指向const char的非const指针。 Tricky语言。

The reason you can set a const char *xyz; at runtime is this declares a non-const pointer to a const char. Tricky language.

所以如果你想要一个int,可以在main()中确定,后来没有改变,你可以写一个getter int xyz (),它返回一个在main()或getter中初始化的静态值。

So if you want an int that can be determined in main() and not changed afterwards, you can write a getter int xyz() that returns a static value that gets initialized in main() or in the getter.

在多个头文件中声明相同的extern变量。)

(BTW, it's not a good idea to declare the same extern variable in more than one header file.)

这篇关于C ++全局外部常量定义在运行时可用跨多个源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:08