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文件中定义及使用

  • 相关阅读:
    Install Postgresql on Ubuntu
    Pytest
    Pytest
    wrk 压力测试
    Objective-C 格式化字符串Format
    安装BeyondCompare on Ubuntu
    eclipse + python + pydev (Pydev安装成功确看不到插件的解决办法)
    如何解决: Ubuntu 系统 adb devices出现no permissions
    oracle rac搭建
    CentOS6.8编译安装LAMP
  • 原文地址:https://www.cnblogs.com/qiangupc/p/3462984.html
Copyright © 2011-2022 走看看