C固定关键字VS C ++私人范围是什么? [英] C static keyword vs C++ private scope?

查看:103
本文介绍了C固定关键字VS C ++私人范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C ++等同放着清单翻译单元本地用C静态功能?结果
例如具有以下 bar.c

 静态无效巴(){
    // ...
}

在C ++中,将这个被写成一个私有成员函数像

 类Foo {
    空巴();
};无效美孚::酒吧(){
    // ...
}

一个私有成员函数隐式地介绍了这个指针作为参数,因此它不是真正媲美C形式静态功能。但即使是私有静态成员函数巴()将在公共接口中可以看出(和保持的链接访问),并且是不具有可比性为好。

虽然这些功能可访问范围似乎是相似的,这些选项并不像好替代提到的C形式静态函数的语法。

是等效的一位不愿透露姓名命名空间的功能,这是对当前翻译单元仅可见?

 命名空间{
    空巴(){
       // ...
    }
}


解决方案

[C]与文件范围内静电功能。

 静态无效巴(){...}

这将创建一个名为函数有内部连杆

[C ++]与文件范围内的静态函数

 静态无效巴(){...}

这将创建一个名为函数有内部连杆

[C ++]具名命名空间

 命名空间{
    空隙栏(){...}
}

这将创建一个名为函数有内部连杆

结论

他们都是相同。我可能会建议使用具名命名空间在C ++中,因为它摆脱了一些静态关键字的超载。但是从你的code做什么角度看,这并不重要。

补充工具栏:?什么是内部链接意味着

在C和C ++中,我们有三种联动为外部 内部否联动。要定义这些,我会从C ++ 2011第一章引述3.5条第2款:


  

一个名字,据说有关联时,它可能意味着同一个对象,引用,函数,类型,模板,命名空间或价值在另一个范围内声明引入了一个名称:


  
  

      
  • 当一个名称具有外部链接,它指的实体可以通过名称从其他翻译单位的范围或同一翻译单元的其他范围引用。

  •   
  • 当一个名称有内部连接,它指的实体可以通过在同一个翻译单元其他范围的名称来称呼。

  •   
  • 当一个名字没有联系,它表示实体不能被从其他范围的名称提及。

  •   

ç2011有类似的语言在6.2.2节第2段:


  

在构成整个程序集的翻译单位和图书馆,与外部连接特定的标识符的每个声明表示同一对象或函数。在一个翻译单元,具有内部链接的标识符的每个声明表示相同对象或功能。不带链接的标识符中的每一个声明表示唯一的实体。


这样有内部链接名称仅在他们中找到的翻译单元可见。

补充工具栏:我们包括如何内部链接在实践工作的例子:

让我们创建2 C ++文件。 bar.cc 将包含只是一个内部联动功能:

 静态无效巴(){}

我们还将创建 main.cc 后,将尝试使用巴()

 的extern无效巴();诠释主(){
    酒吧();
}

如果我们编译这一点,我们的连接器会抱怨。没有命名的功能,我们可以从main.cc翻译单元找到。这是内部联动的预期行为。

 适用于建筑x86_64的未定义符号:
  栏(),从引用:
      在_main主c16bef.o
LD:符号(S)未找到x86_64的架构
铿锵:错误:连接命令,退出code 1(使用-v看看调用)失败

What is the C++ equvalent for translation unit local static function in C?
For example having the following in bar.c:

static void bar() {
    // ...
}

In C++, would this be written as a a private member function like

class foo {
    void bar();
};

void foo::bar() {
    // ...
}

A private member function implicitly introduces the this pointer as parameter, so it's not really comparable to the C style static function. But even a private static member function bar() would be seen in the public interface (and staying accessible for the linker), and isn't comparable as well.

While accessible scope of those functions seems to be similar, these options don't look like good replacements for the mentioned C style static function syntax.

Is the equivalent a function in an unnamed namespace, that's visible to the current translation unit only?

namespace {
    void bar() {
       // ...
    }
}

解决方案

[C] Static function with file scope.

static void bar() { ... }

This will create a function named bar that has internal linkage.

[C++] Static function with file scope

static void bar() { ... }

This will create a function named bar that has internal linkage.

[C++] Unnamed namespace

namespace {
    void bar() { ... }
}

This will create a function named bar that has internal linkage.

Conclusions

They are all identical. I'd probably recommend using the unnamed namespace in C++, because it gets rid of some of the overloading of the static keyword. But from the perspective of what your code does, it doesn't matter.

Sidebar: What does internal linkage mean?

In C and C++, we have three kinds of linkage: External, Internal and No linkage. To define these, I'm going to quote from C++ 2011 Section 3.5 Paragraph 2:

A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope:

  • When a name has external linkage , the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.
  • When a name has internal linkage , the entity it denotes can be referred to by names from other scopes in the same translation unit.
  • When a name has no linkage , the entity it denotes cannot be referred to by names from other scopes.

C 2011 has similar language at Section 6.2.2 Paragraph 2:

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

So names that have internal linkage are only visible in the translation unit that they were found in.

Sidebar: Let's include an example of how internal linkage works in practice:

Let's create 2 c++ files. bar.cc will contain just a function with internal linkage:

static void bar() {}

We'll also create main.cc, which will try to use that bar().

extern void bar();

int main() {
    bar();
}

If we compile this, our linker will complain. there is no function named bar that we can find from the main.cc translation unit. This is the expected behavior of internal linkage.

Undefined symbols for architecture x86_64:
  "bar()", referenced from:
      _main in main-c16bef.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这篇关于C固定关键字VS C ++私人范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆