【问题标题】:Static classes C ++静态类 C++
【发布时间】:2015-03-20 20:33:47
【问题描述】:
 #include"stdafx.h"
 #include"iostream"
 using namespace std;
 static class base{
 public:
    int i = 3;
 };
 int main(){
 base ob;
 system("pause");
 return 0;
 }

请告诉我,类声明中的“静态”是什么?

【问题讨论】:

  • That is not legal。它不应该编译。它最初可能是变量声明的一部分,例如:static class X{...} my_x_variable;,但不在此代码中。
  • It's invalid syntax! 但可能会被其他编译器错误隐藏。
  • @Trinimon:您提供的链接实际上无助于回答 OP 的问题。主要是因为它涵盖了static 的有效使用,并且OP 使用它的方式是无效的。但是,我可以确认它确实在 Visual Studio 2013 中编译(尽管有一个警告 static 被忽略)
  • 一切都编译,没有编译错误。 Visual Studio 2013
  • 使用this online compiler 我得到:main.cpp(6):警告 C4091:'static':当没有声明变量时,'base' 左侧被忽略。阅读您的编译器输出。

标签: c++ class visual-studio-2013 static


【解决方案1】:

“请告诉我,类声明中的“static”是什么意思?谢谢。”

它实际上什么都不做,但被编译器忽略了(看起来只有 VS 2013)。对此发出警告:

source_file.cpp(9) : warning C4091: 'static ' : 
   ignored on left of 'base' when no variable is declared

查看实时编译器示例here please

【讨论】:

  • (一个reference他们的“警告”,他们的编译器可能会忽略无意义的关键字)
  • 其实不只是VS。 Clang 也会编译它。
  • 没有关于clang有多糟糕的声明? :)
  • @BenjaminLindley "...关于 clang 有多糟糕?" 真的没有那么多时间来真正证明 vs MSVC :-P ...
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 2010-09-26
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
相关资源
最近更新 更多