简体   繁体   English

C++:使用带有未命名命名空间的外部链接来使用常量变量

[英]C++: Using external linkage with an unnamed namespace to use a constant variable

My goal is that when I call myFunction from within main, I do not have to pass the SIZE constant.我的目标是,当我从 main 中调用 myFunction 时,我不必传递 SIZE 常量。 The function within myFunction.cpp should then run and simply output the contents of the array.然后应运行 myFunction.cpp 中的函数并简单地输出数组的内容。

I've read some notes on external linkage but I feel like I don't understand it well enough to apply anything in this case.我已经阅读了一些关于外部链接的注释,但我觉得我对它的理解不够好,无法在这种情况下应用任何东西。 Ideally main.cpp shouldn't be changed much other than giving the namespace including SIZE a name if absolutely necessary.理想情况下,除了在绝对必要的情况下为包含 SIZE 的名称空间命名外,不应更改 main.cpp。

main.cpp主程序

#include <iostream>
#include "myFunction.hpp"

namespace
{
    extern const int SIZE = 10;
}

void myFunction(char []);

int main()
{
    char myArray[SIZE] = "123456789";

    myFunction(myArray);
}

myFunction.h myFunction.h

#ifndef MYFUNCTION_H_INCLUDED
#define MYFUNCTION_H_INCLUDED

namespace
{
    extern const int SIZE;
}

#endif // MYFUNCTION_H_INCLUDED

myFunction.cpp myFunction.cpp

#include <iostream>

void myFunction(char myArray[])
{
    for(int i = 0; i < SIZE; ++i)
    {
        std::cout << myArray[i] << " ";
    }
}

With what I have so far I still get the error that SIZE was not declared in that scope (myFunction.cpp).到目前为止,我仍然收到错误,即未在该范围内声明 SIZE (myFunction.cpp)。

Question: What do I have to do to make this example work?问题:我需要做什么才能使这个示例工作? If possible, I'd also love an explanantion of why one would handle sharing SIZE this way.如果可能的话,我也很想解释一下为什么要以这种方式处理共享 SIZE。

Am unnamed namespace tells the compiler that nothing inside is to be shared with another translation unit. Am unnamed namespace 告诉编译器里面的任何东西都不会与另一个翻译单元共享。 (That is, everything inside has internal linkage .) As long as SIZE is in an unnamed namespace , it cannot have external linkage , so it cannot be seen by myFunction() . (也就是说,里面的所有东西都有内部链接。)只要SIZE在一个未命名的命名空间中,它就不能有外部链接,所以它不能被myFunction()看到

If possible, I'd also love an explanantion of why one would handle sharing SIZE this way.如果可能的话,我也很想解释一下为什么要以这种方式处理共享 SIZE。

Since you are working with a toy exercise, my guess is that one would handle sharing SIZE this way to better understand internal and external linkage.由于您正在使用玩具练习,我的猜测是人们会以这种方式处理共享SIZE以更好地理解内部和外部链接。 It's an exercise, not a real-world example.这是一个练习,而不是一个现实世界的例子。

For a more realistic example, maybe a version string would be both simple and good?对于更现实的示例,也许版本字符串既简单又好? Your app might have a version (eg "1.69.8109") that it displays at various points.您的应用程序可能有一个版本(例如“1.69.8109”),它会在不同时间点显示。 This version could be declared as a const string that is used by several source files.这个版本可以声明为一个const string ,供多个源文件使用。 It would go into a (named) namespace to avoid name conflicts.它将进入(命名)命名空间以避免名称冲突。 It could be defined in a source file rather than a header file to limit the amount of re-compilation needed when the version changes.它可以在源文件而不是头文件中定义,以限制版本更改时所需的重新编译量。 (Only the source with the definition would need to be recompiled, not every source that uses the version string.) This might be a reasonable benefit, especially if the version number changes reasonably often, not necessarily just when a new release is being prepared. (只有带有定义的源需要重新编译,而不是每个使用版本字符串的源。)这可能是一个合理的好处,特别是如果版本号经常发生合理的变化,不一定只是在准备新版本时。

(From the comments): "Try to get this to work. You may give the unnamed namespace holding SIZE a name if that helps" (来自评论): “尝试让它发挥作用。如果有帮助,您可以为包含 SIZE 的未命名命名空间命名”

It's funny how often instructions for an exercise will tell you what to do.有趣的是,练习的说明经常会告诉您该做什么。 (Heh, "if".) (呵呵,“如果”。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM