简体   繁体   English

在 C++ 中,哪些链接被授予非局部成员变量?

[英]Which linkages are granted to non-local member variables in c++?

I assume that a class's data members have linkage(or no linkage) like below我假设一个类的数据成员有链接(或没有链接),如下所示
Is that correct?那是对的吗?

class AAA {
public:                          // this doesn't matter

  int var1;                      // no linkage
  const int var2 = 0;            // no linkage

  static int var3;               // external linkage (declaration)
  static const int var4 = 0;     // external linkage (declaration + definition)

};

int AAA::var3 = 0; // external linkage (definition)

First of all I questioned about which linkages are granted to data members首先我质疑哪些链接被授予数据成员
and my assumption was right and MM answered it's OK too我的假设是对的,MM也回答说没关系
I double checked in the standard and yes, linkages are correct like I and MM said above我仔细检查了标准,是的,链接是正确的,就像我和 MM 上面说的那样

But as MM mentioned I misunderstood which one is declaration or definition但正如 MM 提到的,我误解了哪个是声明或定义
It's a bit out of topic but I answer it by myself for someone who read this in future这有点题外话,但我自己回答了将来阅读此内容的人

[1] if it comes to non-static data member [1] 如果是非静态数据成员

struct A {
  int var1;        // definition (initial value is 0)
  int var2 = 100;  // definition (initial value is 100)
};

[2] if it comes to static const [2] 如果是静态常量

struct A {
  static const int var1 = 100; // declaration (initial value is 100)
  static const int var2;       // declaration (initial value is 0)
};

const int A::var1;             // definition (an option for odr-used situation)
const int A::var2 = 10;        // definition

[3] if it comes to static constexpr [3] 如果是静态 constexpr

constexpr data members are interesting and work differently constexpr数据成员很有趣并且工作方式不同
because from C++17 a data member with constexpr implies inline因为从 C++17 开始,带有constexpr的数据成员意味着inline
and inline actually define itinline实际定义它

plus every inline data members in every translation units are guaranteed to have same only one definition加上每个翻译单元中的每个inline数据成员都保证只有一个相同的定义
( https://eel.is/c++draft/depr.static.constexpr ) ( https://eel.is/c++draft/depr.static.constexpr )

struct A {
  static constexpr int n = 5;   // definition (declaration in C++ 2014)
};

constexpr int A::n;             // redundant declaration (definition in C++ 2014)

for example if I have 2 translation units like below例如,如果我有 2 个如下的翻译单元

// foo.cpp
#include <iostream>

class AAA {
public:
    static constexpr int var = 10; 
};

void foo() {
    std::cout << &AAA::var << std::endl;
}

// main.cpp
#include <iostream>

class AAA {
public:
    static constexpr int var = 10; 
};

void foo();

int main() {
    std::cout << &AAA::var << std::endl;
    foo();
}

and result is like below结果如下

$ g++ -std=c++17 main.cpp foo.cpp
$ ./a.out 
0x564ba82a3a54
0x564ba82a3a54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非本地变量在C ++中使用匿名类型警告 - non-local variable uses anonymous type warning in C++ C++ - 创建非局部变量指针的最佳方式 - C++ - Best way to create a non-local variable pointer C ++:将Unix时间转换为非本地时区 - C++: converting Unix time to non-local timezone 为什么不同块中的同名外部局部变量在 c++ 中的编译器之间获得不同的链接? - Why same named extern local variables in different blocks get different linkages between compilers in c++? C ++ - 非本地静态对象与本地静态对象 - C++ - Non-local static object vs local static object 在C ++应用程序(gtkmm)中使用非本地数据/媒体文件 - Using Non-Local Data/Media Files with a C++ Application (gtkmm) c ++在不同翻译单元中交互的非本地静态对象的示例 - c++ example of non-local static objects interacting in different translation units 在C ++(Qt)中使用非本地IP地址设置服务器 - Setting up a server with a non-local IP address in C++ (Qt) C ++类设计:成员变量和局部变量 - C++ class design: member and local variables 与 c 中的链接相比,c++ 链接中的命名空间有什么影响? - What is the impact of namespaces in c++ linkages compared to linkages in c?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM