<i id='FLlWo'><tr id='FLlWo'><dt id='FLlWo'><q id='FLlWo'><span id='FLlWo'><b id='FLlWo'><form id='FLlWo'><ins id='FLlWo'></ins><ul id='FLlWo'></ul><sub id='FLlWo'></sub></form><legend id='FLlWo'></legend><bdo id='FLlWo'><pre id='FLlWo'><center id='FLlWo'></center></pre></bdo></b><th id='FLlWo'></th></span></q></dt></tr></i><div id='FLlWo'><tfoot id='FLlWo'></tfoot><dl id='FLlWo'><fieldset id='FLlWo'></fieldset></dl></div>
<legend id='FLlWo'><style id='FLlWo'><dir id='FLlWo'><q id='FLlWo'></q></dir></style></legend>

  • <small id='FLlWo'></small><noframes id='FLlWo'>

    1. <tfoot id='FLlWo'></tfoot>

        • <bdo id='FLlWo'></bdo><ul id='FLlWo'></ul>
      1. C++ 类实例上的静态成员方法调用

        C++ Static member method call on class instance(C++ 类实例上的静态成员方法调用)

          <legend id='Sp3Wb'><style id='Sp3Wb'><dir id='Sp3Wb'><q id='Sp3Wb'></q></dir></style></legend>
            <tbody id='Sp3Wb'></tbody>
          <tfoot id='Sp3Wb'></tfoot>
        • <small id='Sp3Wb'></small><noframes id='Sp3Wb'>

          <i id='Sp3Wb'><tr id='Sp3Wb'><dt id='Sp3Wb'><q id='Sp3Wb'><span id='Sp3Wb'><b id='Sp3Wb'><form id='Sp3Wb'><ins id='Sp3Wb'></ins><ul id='Sp3Wb'></ul><sub id='Sp3Wb'></sub></form><legend id='Sp3Wb'></legend><bdo id='Sp3Wb'><pre id='Sp3Wb'><center id='Sp3Wb'></center></pre></bdo></b><th id='Sp3Wb'></th></span></q></dt></tr></i><div id='Sp3Wb'><tfoot id='Sp3Wb'></tfoot><dl id='Sp3Wb'><fieldset id='Sp3Wb'></fieldset></dl></div>

                • <bdo id='Sp3Wb'></bdo><ul id='Sp3Wb'></ul>

                • 本文介绍了C++ 类实例上的静态成员方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是一个小测试程序:

                  #include <iostream>
                  
                  class Test
                  {
                  public:
                      static void DoCrash(){ std::cout<< "TEST IT!"<< std::endl; }
                  };
                  
                  int main()
                  {
                      Test k;
                      k.DoCrash(); // calling a static method like a member method...
                  
                      std::system("pause");
                  
                      return 0;
                  }
                  

                  在 VS2008 + SP1 (vc9) 上它编译得很好:控制台只显示TEST IT!".

                  On VS2008 + SP1 (vc9) it compiles fine: the console just display "TEST IT!".

                  据我所知,不应在实例化对象上调用静态成员方法.

                  As far as I know, static member methods shouldn't be called on instanced object.

                  1. 我错了吗?从标准的角度来看,此代码是否正确?
                  2. 如果正确,为什么会这样?我不知道为什么允许这样做,或者是为了帮助在模板中使用静态与否"方法?

                  推荐答案

                  标准规定不需要通过实例调用方法,并不代表你不能这样做.甚至还有一个使用它的例子:

                  The standard states that it is not necessary to call the method through an instance, that does not mean that you cannot do it. There is even an example where it is used:

                  C++03, 9.4 静态成员

                  C++03, 9.4 static members

                  类 X 的静态成员 s 可以使用限定 id 表达式 X::s;它是没有必要使用类成员访问语法(5.2.5)来引用到静态成员.静态成员可能使用类成员访问语法来引用,其中如果对象表达式是评价.

                  A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object-expression is evaluated.

                  class process {
                  public:
                     static void reschedule();
                  };
                  
                  process& g();
                  
                  void f()
                  {
                     process::reschedule(); // OK: no object necessary             
                     g().reschedule(); // g() is called
                  }
                  

                  这篇关于C++ 类实例上的静态成员方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  • <bdo id='JByop'></bdo><ul id='JByop'></ul>

                    <legend id='JByop'><style id='JByop'><dir id='JByop'><q id='JByop'></q></dir></style></legend>

                    1. <i id='JByop'><tr id='JByop'><dt id='JByop'><q id='JByop'><span id='JByop'><b id='JByop'><form id='JByop'><ins id='JByop'></ins><ul id='JByop'></ul><sub id='JByop'></sub></form><legend id='JByop'></legend><bdo id='JByop'><pre id='JByop'><center id='JByop'></center></pre></bdo></b><th id='JByop'></th></span></q></dt></tr></i><div id='JByop'><tfoot id='JByop'></tfoot><dl id='JByop'><fieldset id='JByop'></fieldset></dl></div>

                            <tfoot id='JByop'></tfoot>

                            <small id='JByop'></small><noframes id='JByop'>

                              <tbody id='JByop'></tbody>