Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C++中使用静态方法和静态函数?_C++ - Fatal编程技术网

为什么C++中使用静态方法和静态函数?

为什么C++中使用静态方法和静态函数?,c++,C++,在单例模式中,我们使用的是静态方法。我们想知道单例模式中静态方法和函数调用的流程,以及为什么我们在单例模式中使用静态。创建静态函数和静态变量的原因是,我们不希望编译器在程序运行时分配可变的内存。我们不希望它改变 静态方法提供了一个类公共API,我们可以调用它来获取要使用的值的唯一副本 // Example of a model object and a static get the only copy method. Model::getInstance();

在单例模式中,我们使用的是静态方法。我们想知道单例模式中静态方法和函数调用的流程,以及为什么我们在单例模式中使用静态。

创建静态函数和静态变量的原因是,我们不希望编译器在程序运行时分配可变的内存。我们不希望它改变

静态方法提供了一个类公共API,我们可以调用它来获取要使用的值的唯一副本

// Example of a model object and a static get the only copy method.
Model::getInstance();