C++ 类和结构体内外的Static

发布时间 2023-08-20 16:32:31作者: OrzMiku

类和结构体外的Static

这里的static指的是在类外的static,意味着你要声明的是static的符号,链接将只在内部。(换句话说作用域只在他所在的.cpp文件)。

下面是几组例子来理解:

例子1:

main.cpp

#include <iostream>

using namespace std;

void fun(){
    cout << "fun() in main.cpp" << endl;
}

int main()
{
    fun();
    return 0;
}

static.cpp

#include<iostream>

using namespace std;

static void fun(){
    cout << "static fun() in static.cpp" << endl;
}

编译将不会报错,运行结果:

fun() in main.cpp

此时static.cpp中的fun()因为被static修饰,只在static.cpp中有效。

例子2:

main.cpp

#include <iostream>

using namespace std;

void fun(){
    cout << "fun() in main.cpp" << endl;
}

int main()
{
    fun();
    return 0;
}

static.cpp

#include<iostream>

using namespace std;

void fun(){
    cout << "static fun() in static.cpp" << endl;
}

编译后报错,无法运行,函数名重复了

例子3:

main.cpp

#include <iostream>

using namespace std;

int main()
{
    fun();
    return 0;
}

static.cpp

#include<iostream>

using namespace std;

static void fun(){
    cout << "static fun() in static.cpp" << endl;
}

编译后报错,无法运行,找不到fun()函数

例子4:

main.cpp

#include <iostream>

using namespace std;

extern void fun();

int main()
{
    fun();
    return 0;
}

static.cpp

#include<iostream>

using namespace std;

static void fun(){
    cout << "static fun() in static.cpp" << endl;
}

编译后报错,无法运行,找不到fun()函数(static.cpp中的fun()函数只在static.cpp生效,不能被main.cpp extern)

例子5:

main.cpp

#include <iostream>

using namespace std;

extern void fun();

int main()
{
    fun();
    return 0;
}

static.cpp

#include<iostream>

using namespace std;

void fun(){
    cout << "fun() in static.cpp" << endl;
}

编译不会报错,运行结果:

fun() in static.cpp

static.cpp的fun()没被static修饰,可以被extern,此时main.cpp中的fun()调用的是static.cpp中的fun()

类和结构体中的Static

类/结构体中的static,表明这个符号在这个类的所有对象共用。不属于任何一个对象。调用时直接用命名空间来访问这个符号。(函数内的静态变量不做讨论,和C语言是一样的)

下面是一些例子:

没有使用static:

#include <iostream>

using namespace std;

class Player{
public:
    int x = 0, y = 0;
    void move(int xa, int ya){
        cout << "move from (" << x << "," << y << ") to \
(" << x+xa << "," << y+ya << ")" << endl;
        x += xa;
        y += ya;
    };
};

int main()
{
    Player player;
    player.move(2,7);
    player.move(3,5);
    return 0;
}

静态成员变量:

#include <iostream>

using namespace std;

class Player{
public:
    static int x, y;
    void move(int xa, int ya){
        cout << "move from (" << x << "," << y << ") to \
(" << x+xa << "," << y+ya << ")" << endl;
        x += xa;
        y += ya;
    };
};

int Player::x;
int Player::y;

int main()
{
    Player player1;
    Player player2;
    Player::x = 3;
    Player::y = 6;
    Player::y = 6;
    player1.move(2,7);
    player2.move(3,5);
    return 0;
}

静态成员方法

  • 注意,类中静态方法不能访问非静态成员。
#include <iostream>

using namespace std;

class Player{
public:
    int x = 0, y = 0;
    static void move(Player& player, int xa, int ya){
        cout << "move from (" << player.x << "," << player.y << ") to \
(" << player.x+xa << "," << player.y+ya << ")" << endl;
        player.x += xa;
        player.y += ya;
    };
};

int main()
{
    Player player;
    Player::move(player, 2, 7);
    Player::move(player, 3, 5);
    return 0;
}