【问题标题】:what is the equivalent of :: operator in D?D 中 :: 运算符的等价物是什么?
【发布时间】:2015-08-04 10:40:52
【问题描述】:

我刚刚开始学习 D。在 C++ 中,如果全局变量和局部变量具有相同的名称,则有 ::(作用域解析运算符)可以从函数中访问全局变量。但是如何在 D 语言中做到这一点呢?考虑这个程序。

import std.stdio;
int a;
int main(string[] args)
{
    int a=3;
    writeln("D is nice");
    static int i;
    writeln("value of i is: ",i);
    writeln("value of a is: ",a);
   // writeln("value of ::a is: ",::a); compiler error here
    return 0;
}

如何在 main() 函数中打印全局变量 a 的值? D 是否提供这种运算符?

【问题讨论】:

    标签: global-variables d name-lookup


    【解决方案1】:

    D 使用前导点:

    writeln("value of .a is: ",.a);
    

    在规范中:http://dlang.org/module.html - “模块范围运算符”部分

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 2023-04-10
      • 2023-03-16
      • 2017-08-19
      相关资源
      最近更新 更多