The normal C + + language standard currently (up to c++14) should not yet support this invocation method. At present, Microsoft seems to be in its VC + + implementation of a standard called C + +/CLI, it is possible to support such a call, if it is necessary to use this method of invocation, you should also use VS2013 try to compile and run a bit.
In fact, the static member function of a class in the C + + language should itself be the behavior of the collective of all such objects, that is, not an object can have or be implemented, whereas a non-static member function should be an object's own action behavior, which is not related to other objects of this class or even the whole class. Is the behavior that an object can accomplish 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 that a member function does not need to refer to a non-static member of this object, but must also be an object's own behavior (that is, declared as a non-static member function). If there really is a member function that does not refer to a non-static member, it is better to declare it as a static member function, so that you can safely compile and avoid porting problems.
In other words, non-static member functions always have an implied parameter, which is the this pointer. Disassembly analysis also reveals that calls to non-static member functions belong to special thiscall, meaning that a this pointer is always passed in. Static member functions, like functions outside the class, are normally called after compilation, do not get the this pointer, and therefore cannot access non-static members (because references to non-static members are always done through the this pointer). So whether a function can be called by a class name depends primarily on whether it requires the compiler to pass in the this pointer (to see the compiled code, the call at the source level does not see the incoming this pointer).
If you really want to invoke a non-static member function without an instance, you can use the following method (provided that you must conform to the condition you are proposing that you do not access any non-static members, which can cause memory read-write exceptions if it accesses non-static members):
// 假设要引用的类类型为 TargetType, 成员函数为 void TargetType::TargetFunc();// C++11 版:static_cast<TargetType *>(nullptr)->TargetFunc();// C++98/03 版:reinterpret_cast<TargetType *>(0)->TargetFunc();// 如果使用 C 风格的类型转换操作符:((TargetType *) 0)->TargetFunc();
Transferred from: https://segmentfault.com/q/1010000005650649/a-1020000005651548
Why is it possible to access non-static member functions directly in C + + with the class name?