search

Home  >  Q&A  >  body text

静态函数 - c++中,可以用类名直接访问非静态成员函数?

如题,看到一篇文章,说只要非静态成员函数中没有使用非静态成员变量,就可以通过类名和域运算符来直接调用非静态成员函数,而不需要实例来调用,是这样吗?
编译器为vs2013

PHP中文网PHP中文网2901 days ago841

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-17 13:49:03

    It’s strange that the class name can access non-static members, so how do I distinguish which non-static member is being accessed? Static can be understood as variables shared by all objects created with this class.

    reply
    0
  • 阿神

    阿神2017-04-17 13:49:03

    As long as non-static member variables are not used in non-static member functions

    What this sentence means is that there is no call to this pointer in the non-static member function. The non-static member function that does not call this is actually not much different from the static member.

    You can first convert a nullptr into the corresponding class type, and then call this non-static memrber function, as shown below:

    class A {
    public:
        void printHello() {
            cout << "hello" << endl;
        }
    };
    
    void test() {
        A *p = nullptr;
        p.printHello();
    }

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:49:03

    The formal C++ language standard currently (as of C++14) should not support this calling method.
    Currently, Microsoft seems to be promoting a standard called C++/CLI in its VC++, which may support this kind of calling. If you must use this calling method, you should try to compile and run it with VS2013.

    In fact, the static member function of a class in the C++ language itself should be the collective behavior of all objects of this type, that is to say, it is not something that a certain object can have or implement; the non-static member function should be The action behavior of a certain object has little to do with other objects of this class or even the entire class. It is a behavior that the object can complete by relying on its own data and function parameters.
    Based on the above discussion, we can see that it is difficult to have a requirement such that a member function does not need to refer to non-static members of this object, and at the same time it must be the behavior of an object itself (that is, declared as a non-static member function). If there really is a member function that does not reference non-static members, it is better to directly declare it as a static member function, so that it can be compiled without any problems and avoid portability problems.

    As a aside, non-static member functions always have an implicit parameter, which is the this pointer. Through disassembly analysis, we can also find that calls to non-static member functions belong to the special thiscall, which means that a this pointer is always passed in. Static member functions, like functions outside the class, are ordinary calls after compilation and will not get this pointer, so it is impossible to access non-static members (because references to non-static members are always done through this pointer). Therefore, whether a function can be called through the class name mainly depends on whether it requires the compiler to pass in the this pointer (it depends on the compiled code, the call at the source code level cannot see the passed this pointer).

    If you really want to call a non-static member function without an instance, you can use the following method (provided that it must meet the condition you proposed, that is, it does not access any non-static members. If it accesses non-static members, Static members may cause memory read and write exceptions):

    // 假设要引用的类类型为 TargetType, 成员函数为 void TargetType::TargetFunc();
    // C++11 版:
    static_cast<TargetType *>(nullptr)->TargetFunc();
    // C++98/03 版:
    reinterpret_cast<TargetType *>(0)->TargetFunc();
    // 如果使用 C 风格的类型转换操作符:
    ((TargetType *) 0)->TargetFunc();

    reply
    0
  • Cancelreply