zoukankan      html  css  js  c++  java
  • static成员变量的使用

    最近自己在研究C++中static成员变量的注意事项,只是自我研究,仅供参考:

    例如:

    file1.h

    #ifndef FILE_1_H
    #define FILE_1_H
    
    void func1(void);
    void func2(void);
    static int func(void);
    #endif // FILE_1_H
    

    file1.cpp

    #include "file1.h"
    #include <iostream>
    using namespace std;
    
    static int a;
    void func1(void)
    {
        cout << "a = " << a << endl;
        cout << "call a:" << endl;
        cout << "a = " << a << endl;
        a++;
        return;
    }
    void func2(void)
    {
        cout << "call func2:" << endl;
        cout << "a = " << a << endl;
        a++;
        return;
    }
    
    void func(void)
    {
        cout << "call static func" << endl;
        return;
    }
    

      编译错误如下:

    C:UsersEADEFJIcodeblocksProjects1007file1.cpp||In function `void func()':|
    C:UsersEADEFJIcodeblocksProjects1007file1.cpp|23|error: new declaration `void func()'|
    C:UsersEADEFJIcodeblocksProjects1007file1.h|6|error: ambiguates old declaration `int func()'|
    C:UsersEADEFJIcodeblocksProjects1007file1.cpp|23|warning: 'void func()' defined but not used|
    ||=== Build finished: 2 errors, 1 warnings (0 minutes, 0 seconds) ===|

    所以,最好不要在头文件中定义(初始化)某个静态数据成员或静态成员函数,而应该在相应的.cpp文件中定义及使用

  • 相关阅读:
    20151124 Jquery UI form 表单变成dialog
    如何创建windows xp 虚拟机
    mySQL 从删库到跑路
    CF962D Merge Equals
    hihocoder1718 最长一次上升子序列
    2018微软实习笔试一道dp题目总结
    poj3783 Balls
    CF961E Tufurama
    蓝桥杯 高僧斗法
    蓝桥杯 国王的烦恼
  • 原文地址:https://www.cnblogs.com/qiangupc/p/3462984.html
Copyright © 2011-2022 走看看