关于 extern 和 const 组合变量的问题

发布于 2024-11-28 03:43:31 字数 538 浏览 1 评论 0原文

我在互联网上搜索了 const + extern,但对于我的问题似乎没有一个很好的答案。

const 单独意味着内部链接,但是如果我想在编译单元之间共享 const 变量。 extern 是最好的选择吗?

常见的解决方案是:

//g.h
extern const int MAX;

// g.c
extern const int MAX = 3;

但是,这个解决方案有一个缺点,如下所示:

// Say, I want to use this MAX in the same header file.
// g.h
extern const int MAX;
class AClass
{
public: 
    AClass (): i(MAX){}
private:
    int i;
};

编译器会抱怨:“错误C2057:预期的常量表达式”。

有解决办法吗?

I googled const + extern on the internet, but it seems there isn't really a good answer for my question.

const alone means internal linkage, but if I want to share a const variable among compilation units. Is extern the best choice?

Common solution would be:

//g.h
extern const int MAX;

// g.c
extern const int MAX = 3;

However, this solution has a drawback, like below:

// Say, I want to use this MAX in the same header file.
// g.h
extern const int MAX;
class AClass
{
public: 
    AClass (): i(MAX){}
private:
    int i;
};

Compiler will complain like:"error C2057: expected constant expression".

Is there a solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你的呼吸 2024-12-05 03:43:31

如果您希望能够在编译时使用常量(即通过它调整数组大小,而不使用 VLA),则必须在编译时知道它,即它不能具有外部链接。

但是,您可以只在头文件中声明常量,并将其提供给包括它在内的任何人。不过,这不会产生与外部链接完全相同的效果。

// a.h
const int MAX = 3;

// a.cpp
#include "a.h"
int b[a];

if you want to be able to use your constant at compile time (i.e. size an array by it, without using VLA) it has to be known at compile time, i.e. it cannot have external linkage.

However, you could just declare your constant in your header file, and make it available to anyone including it. Still, that won't have the exact same effect as an external linkage.

// a.h
const int MAX = 3;

// a.cpp
#include "a.h"
int b[a];
染年凉城似染瑾 2024-12-05 03:43:31

对于常量整数,一个简单的解决方案是使用枚举:

// g.h
enum { MAX = 3; }

// g.c

#include "g.h"
static char buf[MAX];

您将无法获取 MAX 的地址,但反过来您可以以零内存成本获得该地址。

An easy solution for constant integers is to use enums:

// g.h
enum { MAX = 3; }

// g.c

#include "g.h"
static char buf[MAX];

You won't be able to take the address of MAX, but in turn you get this at zero memory cost.

神经大条 2024-12-05 03:43:31
extern const int MAX;
int i[MAX];

做不到。你可以做一些像

const int MAX = ReadAnIntegerFromTheConsole();

完全有效和合法的事情,但是哎呀——不是一个恒定的表达。

extern const int MAX;
int i[MAX];

Can't be done. You could do something like

const int MAX = ReadAnIntegerFromTheConsole();

Perfectly valid and legal, but whoopsies- not a constant expression.

耳钉梦 2024-12-05 03:43:31

const 单独表示内部链接

这是不正确的,static 表示内部链接,const 只是表示对象不能变异。尝试声明一个变量,因为

extern static int foo;

您的编译器会抱怨链接冲突。要在翻译单元之间共享 const ,请完全按照您的建议进行操作。

在标题中

extern const int MAX;

在源文件中

const int MAX = 10; // note you can omit the extern here

const alone means internal linkage

This is not correct, static indicates internal linkage, const just says the object cannot mutate. Try declaring a variable as

extern static int foo;

Your compiler will complain about conflicting linkage. To share a const between translation units do exactly what you've suggested.

In the header

extern const int MAX;

In the source file

const int MAX = 10; // note you can omit the extern here
陪你到最终 2024-12-05 03:43:31

这是一个可以解决您的问题的工作示例。总之,在头文件中将数组大小定义为常量。在另一个头文件中将数组声明为 extern。在下面的示例中,我将数组引用为 extern,而不使用该数组的包含文件。

array_size.hpp

const unsigned int MAX_ARRAY_SIZE = 16;

array.cpp

#include "array_size.hpp"
int array[MAX_ARRAY_SIZE];

ma​​in.cpp

#include "array_size.hpp"

// Reference the array from array.cpp
extern int array[MAX_ARRAY_SIZE];

int main(void)
{
  array[1] = 7;
  return 0;
}

*array_size.hpp* 文件定义大小,该标识符可以在其他翻译单元中使用通过包含标题。

我在 Cygwin 上编译使用:

g++ -I. -o array.exe main.cpp array.cpp

Here is a working example that may solve your issue. In summary, define the array size as a constant in a header file. In another header file declare the array as extern. In the example below I reference the array as extern without using an include file for the array.

array_size.hpp

const unsigned int MAX_ARRAY_SIZE = 16;

array.cpp

#include "array_size.hpp"
int array[MAX_ARRAY_SIZE];

main.cpp

#include "array_size.hpp"

// Reference the array from array.cpp
extern int array[MAX_ARRAY_SIZE];

int main(void)
{
  array[1] = 7;
  return 0;
}

The *array_size.hpp* file defines the size, the identifier can be used in other translation units by including the header.

I compiled on Cygwin using:

g++ -I. -o array.exe main.cpp array.cpp
初见 2024-12-05 03:43:31

为什么不直接使用#define?

#define MAX 3

Why not just use a #define?

#define MAX 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文