【问题标题】:C++ Namespaces and ClassesC++ 命名空间和类
【发布时间】:2015-07-27 13:54:19
【问题描述】:

我想在不调用类的情况下使用类中的常量,只需调用常量,如下所示:

头文件(.h)

class myClass{

   public: 
      const static char CHAR_S = '<';
}

源文件 (.cpp)

using namespace myClass;
char separator = CHAR_S; //I want to call the constant without using the class (myClass::CHAR_S)

我该怎么做?

最小完整示例:

class MyClass {
    public:
    static const char CHAR_S = '<';
};

int main() {
    using namespace MyClass;
    char separator = CHAR_S;
    return 0;
}

导致以下编译器错误:

main.cpp: In function 'int main()':
main.cpp:7:21: error: 'MyClass' is not a namespace-name
     using namespace MyClass;
                     ^
main.cpp:7:28: error: expected namespace-name before ';' token
     using namespace MyClass;
                            ^
main.cpp:8:22: error: 'CHAR_S' was not declared in this scope
     char separator = CHAR_S;
                      ^
main.cpp:8:10: warning: unused variable 'separator' [-Wunused-variable]
     char separator = CHAR_S;
          ^

【问题讨论】:

  • 应该是const static char CHAR_S = '&lt;'; 而不是"&lt;"
  • 是的,只是在这个问题上弄错了,在我的代码中是正确的。
  • 它也应该是class 而不是Class。 CHAR_S 应该是公开的。
  • 请注意,在 C++ 中,CHAR_S 是一个不好的常量名称,因为 ALL_UPPERCASE 几乎总是表示宏。你应该给它一个不同的名字。

标签: c++ class namespaces constants


【解决方案1】:

类不是命名空间。如果没有通过类名限定它,则不能引用 CHAR_S。

要做你想做的事,你可以简单地将静态变量放在命名空间中:

namespace MyClass {
    static const char CHAR_S = '<';
} // namespace MyClass

int main() {
    using namespace MyClass;
    char separator = CHAR_S;
    return 0;
}

现场示例:http://coliru.stacked-crooked.com/a/74fcfee559cc1390

显然,调用命名空间 MyClass 会产生误导(因为它不是类),所以请选择一个更好的名称。

【讨论】:

    【解决方案2】:

    myClass 不是命名空间,你不能这样使用它。我想你想要这样的东西:

    namespace constants{
        const static char CHAR_S = '<';
    };
    
    using constants::CHAR_S;
    int main(int argc, char ** argv)
    {
        char a = CHAR_S;
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      根据 C++ 标准(3.3.7 类范围)

      2 类成员的名称只能按如下方式使用

      ——在其类(如上所述)或派生类的范围内 (第 10 条)从其类中,

      ——在 .运算符应用于其类型的表达式 类 (5.2.5) 或从其类派生的类,

      — 在 -> 运算符应用于指向其类对象的指针之后 (5.2.5) 或从其类派生的类,

      — 在 :: 范围解析运算符 (5.1) 之后应用于名称 它的类或从它的类派生的类。

      所以我认为唯一的方法是引入另一个变量,该变量将引用类数据成员或将具有其值。例如

      #include <iostream>
      
      struct A
      {
          const static char CHAR_S = '<';
      };
      
      const char A::CHAR_S;
      const char &CHAR_S = A::CHAR_S;
      
      int main()
      {
          std::cout << CHAR_S << std::endl;
      }    
      

      【讨论】:

        【解决方案4】:

        简单的技巧;参考重新定义它。这是一个例子。

        头文件.h

        #ifndef __HEADER__
        #define __HEADER__
        
        class MyClass
        {
        public:
            const static char CHAR_S = '<';
            MyClass();
            ~MyClass();
        };
        
        #endif
        

        类.cpp

        #include "MyClass.h"
        const static char &CHAR_S = MyClass::CHAR_S;
        
        MyClass::MyClass()
        {
        }
        MyClass::~MyClass()
        {
        }
        

        【讨论】:

          猜你喜欢
          • 2013-09-29
          • 2011-06-14
          • 1970-01-01
          • 2023-04-10
          • 2018-02-22
          • 1970-01-01
          • 1970-01-01
          • 2020-05-02
          • 2022-12-31
          相关资源
          最近更新 更多