简体   繁体   English

C++,使用 class 名称调用 function

[英]C++, call function using class name

In C++ how may I write a function that is being called using the class name?在 C++ 中,我如何编写使用 class 名称调用的 function? for example if I have a class called test I want to call a function called calc like this:例如,如果我有一个名为test的 class 我想调用一个名为calc的 function,如下所示:

test::calc();

and not via an object of the class.而不是通过 class 的 object。

class test{
public:
   static void calc(){ /*do stuff */ }
};

See static membersstatic 成员

 class Test {
 public:
     static void calc() { /* ... */ }
 };

 int main() 
 {  
    Test::calc();
 }

Essentially an ordinary function within the namespace of the class.本质上是 class 命名空间内的普通 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM