在类定义中定义静态常量整数成员

2023-12-03C/C++开发问题
4

本文介绍了在类定义中定义静态常量整数成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的理解是,C++ 允许在类中定义静态常量成员,只要它是整数类型即可.

My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.

那么,为什么下面的代码会给我一个链接器错误?

Why, then, does the following code give me a linker error?

#include <algorithm>
#include <iostream>

class test
{
public:
    static const int N = 10;
};

int main()
{
    std::cout << test::N << "
";
    std::min(9, test::N);
}

我得到的错误是:

test.cpp:(.text+0x130): undefined reference to `test::N'
collect2: ld returned 1 exit status

有趣的是,如果我注释掉对 std::min 的调用,代码编译和链接就好了(即使 test::N 在前一行也被引用).

Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).

知道发生了什么吗?

我的编译器是 Linux 上的 gcc 4.4.

My compiler is gcc 4.4 on Linux.

推荐答案

我的理解是,C++ 允许在类中定义静态常量成员,只要它是整数类型即可.

My understanding is that C++ allows static const members to be defined inside a class so long as it's an integer type.

你说得对.您可以在类声明中初始化静态常量积分,但这不是定义.

You are sort of correct. You are allowed to initialize static const integrals in the class declaration but that is not a definition.

有趣的是,如果我注释掉对 std::min 的调用,代码编译和链接就好了(即使 test::N 在前一行也被引用).

Interestingly, if I comment out the call to std::min, the code compiles and links just fine (even though test::N is also referenced on the previous line).

知道发生了什么吗?

std::min 通过常量引用获取其参数.如果按值取它们,您就不会遇到这个问题,但由于您需要一个参考,因此您还需要一个定义.

std::min takes its parameters by const reference. If it took them by value you'd not have this problem but since you need a reference you also need a definition.

这是章节/诗句:

9.4.2/4 - 如果 static 数据成员是 const 整数或 const 枚举类型,它在类定义中的声明可以指定一个常量初始化器,它应该是一个整型常量表达式(5.19).在这种情况下,成员可以出现在整数常量表达式中.如果在程序中使用该成员,则该成员仍应在命名空间范围内定义,并且命名空间范围定义不应包含初始化器.

9.4.2/4 - If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

有关可能的解决方法,请参阅 Chu 的回答.

See Chu's answer for a possible workaround.

这篇关于在类定义中定义静态常量整数成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6