C++ static member functions

今天复习老师昨天讲的static member functions。总觉得这玩意儿存在真是莫名其妙,度娘之,发现网上讲的都十分不清楚。还是用Google从米国网站找到了答案。

class Something

{

private:

static int s_nValue;

};

int Something::s_nValue = 1; // initializer

int main()

{

// how do we access Something::s_nValue?

}

  

In this case, we can‘t access Something::s_nValue directly from main(), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access s_nValue, we‘d then need to instantiate an object of the class type to use the function! We can do better. In this case, the answer to the problem is that we can also make member functions static.

Like static member variables, static member functions are not attached to any particular object. Here is the above example with a static member function accessor:

class Something

{

private:

static int s_nValue;

public:

static int GetValue() { return s_nValue; }

};

int Something::s_nValue = 1; // initializer

int main()

{

std::cout << Something::GetValue() << std::endl;

}

  

以上来自http://www.learncpp.com/cpp-tutorial/812-static-member-functions/

所以说,static member functions纯属是为了避免访问私有static变量再新建对象调用方法修改值的麻烦。

但我记得老师说static还可以通过对象来修改非static的值。用以下代码测试了下

#include <iostream>

using namespace std;

class StaticFunction{

public:static int y;

     int z;

     static void operate(int x){ y = x; };

     static void operate(StaticFunction M, int x){ M.z= x; }

};

int StaticFunction::y = 0;

int main(){

    StaticFunction::operate(5);

    cout << StaticFunction::y << endl;

    StaticFunction example;

    StaticFunction::operate(example, 6);

    cout << example.z << endl;

    cin.get();

}

  

结果每次VS2013都提示z未初始化。开始还以为是没有拷贝构函的缘故,又是写构函,又是新建类,折腾了半天还是没有解决问题。最后,终于—

static void operate(StaticFunction M, int x){ M.z= x; }

  

这明明就和example的值没什么关系,在M前面加了个麻花(&),问题解决。。。TAT我的时间。。。自己还是太年轻了。。

时间: 2024-10-23 21:11:01

C++ static member functions的相关文章

Virtual Member Functions &amp; Static Member Function

如果一个 normalize() 是一个 virtual member function, 那么以下的调用: ptr->normalize(); 将会被内部转化为: (*ptr->vptr[1])(ptr); 其中:vptr 表示由编译器生成的指针, 指向 virtual table, 它被安插在每一个声明有(或继承自) virtual functinos 的 class object 中. 事实上其名称也会被 mangled, 因为在一个复杂的 class 派生体系中, 可能存在多个 vpt

C++ Member Functions的各种调用方式

[1]Nonstatic Member Functions(非静态成员函数) C++的设计准则之一就是:nonstatic member function至少必须和一般的nonmember function有相同的效率.也就是说,如果我们要在以下两个函数之间作选择: float magnitude3d(const Point3d* _this) {...}; float Point3d::magnitude3d() const {...}; 那么选择member function不应该带来什么额

函数的效能 &amp; 指向 Member Functions 的指针与其效能

nonmember friend 或 nonstatic member 或 static member 函数都会被转化为相同的形式, 因此三者的效率完全相同.另外, inline member function 的效率一直是最高的(前提是简单操作), 优化后的效率更是高, 这是因为编译器会将被视为不变的表达式提到循环之外, 因此只计算一次, inline 函数不只能够节省一般函数所调用的负担, 也提供程序额外的优化机会.virtual function 和 多重继承 的效率要低于一般函数, 要再

Const member functions in C++

Recently, I've started to review and learn C++ techniques. During the learning process, I followed a learn-by-example methodology, which I consider to be very effective. From now on, I'll use this blog to explain key points and interesting topics in

转:C语言中的static变量和C++静态数据成员(static member)

转自:C语言中的static变量和C++静态数据成员(static member) C语言中static的变量:1).static局部变量        a.静态局部变量在函数内定义,生存期为整个程序运行期间,但作用域与自动变量相同,只能在定义该变量的函数内使用.退出该函数后, 尽管该变量还继续存在,但不能使用它.        b.对基本类型的静态局部变量若在说明时未赋以初值,则系统自动赋予0值.而对自动变量不赋初值,则其值是不定的.2).static全局变量        全局变量本身就是静

C++ 之const Member Functions

Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter list of an ordinary member function is to modify the type of the implicit this pointer. By default, the type of this is a const pointer to the nonconst v

C++: static member have to be defined seperately

如果这样写c++的单例: class Singleton { private: static Singleton * singleton; private: Singleton() { } public: static Singleton *getinstance() { if(singleton == NULL) { Singleton::singleton = new Singleton(); } return singleton; } }; int main() { Singleton *

static 和 no static Member function学习

以下是做实验的一段代码: #include <iostream> using namespace std; typedef void (*p)(); class Object { public: static void s_fun_1() { cout << "static function 1\n"; } void fun_1() {cout << "no static function 1\n";} }; typedef vo

C++对象模型——Virtual Member Functions (虚拟成员函数)(第四章)

4.2 Virtual Member Functions (虚拟成员函数) 已经看过了 virtual function的一般实现模型:每一个 class 有一个 virtual table,内含该 class 中有作用的 virtual function的地址,然后每个object有一个vptr,指向 virtual table的所在. 为了支持 virtual function机制,必须首先能够对多态对象有某种形式的"执行期类型判断法(runtime type resolution)&quo