最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++的靜態(tài)成員變量和靜態(tài)成員函數(shù)你了解多少

 更新時間:2022年02月18日 14:34:17   作者:EJoft  
這篇文章主要為大家詳細(xì)介紹了C++的靜態(tài)成員變量和靜態(tài)成員函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

靜態(tài)成員變量

這里先引用GeeksforGeeks的一段內(nèi)容:

Static data members are class members that are declared using static keywords. A static member has certain special characteristics. These are:

  • Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
  • It is initialized before any object of this class is being created, even before main starts.
  • It is visible only within the class, but its lifetime is the entire program

語法: static data_type data_member_name;

靜態(tài)變量在任何類對象創(chuàng)建前初始化

我們看代碼示例,一碼勝千言

#include <iostream>
using namespace std;
class A {
public:
    A() {
        cout << "A constructed" << endl;
    }
};
class B {
    static A a;
public:
    B() {
        cout << "B constructed" << endl;
    }
};
int main() {
    B b;
    return 0;
}
// output
B constructed

我們看到B類有一個靜態(tài)成員A類,但是在創(chuàng)建B的對象時并沒有調(diào)用A的構(gòu)造函數(shù),原因很簡單,即在類B中僅僅聲明(declare)了靜態(tài)類A,但沒有在類外定義(define)它。 如果我們在靜態(tài)成員變量定義前使用它,那么會編譯報(bào)錯,這和代碼塊中的靜態(tài)變量不同,代碼塊中的靜態(tài)變量會有常量初始化的過程,代碼示例如下。

#include <iostream>
using namespace std;
class A {
public:
    int x;
    A() {
        cout << "A constructed" << endl;
    }
};
class B {
    static A a;
public:
    B() {
        cout << "B constructed" << endl;
    }
    static A getA() {return a;}
};
int main() {
    B b;
    // A a = b.getA(); // ERROR Compiler Error: undefined reference to `B::a' 
    static int n;
    cout << n << endl; // ok 0
    return 0;
}

定義靜態(tài)成員變量

我們在類內(nèi)定義靜態(tài)成員變量時需要 static,在類外定義鏡頭成員變量時不用 static,語法如下。

class X { static int n; }; // declaration (uses 'static')
int X::n = 1;              // definition (does not use 'static')

這里需要注意幾點(diǎn):

  • const靜態(tài)成員變量無法在類內(nèi)初始化
  • 靜態(tài)成員變量只能在方法外定義,且一定要定義完才能對起引用。

我們考慮下為什么不能在聲明中初始化靜態(tài)變量,這是因?yàn)槁暶髅枋鰜砣绾畏峙鋬?nèi)存,但不分配內(nèi)存。這里我們還是使用上面代碼的例子來說明。

using namespace std;
class A {
public:
    int x;
    A() { cout << "A's constructor called " << endl; }
};
class B {
    static A a;
public:
    B() { cout << "B's constructor called " << endl; }
    static A getA() { return a; }
};
A B::a; // definition of a
int main() {
    B b1, b2, b3;
    A a = b1.getA();
    cout << a.x << endl; // 0
    return 0;
}

output

A's constructor called 
B's constructor called 
B's constructor called 
B's constructor called 
0

從上述結(jié)果我們也可以看出來靜態(tài)成員變量確實(shí)在創(chuàng)建類對象之前初始化。

使用靜態(tài)成員變量

有兩種方法可以引用靜態(tài)成員變量,<類對象名>.<靜態(tài)數(shù)據(jù)成員名> 或 <類類型名>::<靜態(tài)數(shù)據(jù)成員名>

To refer to a static member m of class T, two forms may be used: qualified name T::m or member access expression E.m or E->m, where E is an expression that evaluates to T or T* respectively. When in the same class scope, the qualification is unnecessary:

struct X
{
    static void f(); // declaration
    static int n;    // declaration
};
X g() { return X(); } // some function returning X
void f()
{
    X::f();  // X::f is a qualified name of static member function
    g().f(); // g().f is member access expression referring to a static member function
}
int X::n = 7; // definition
void X::f() // definition 
{ 
    n = 1; // X::n is accessible as just n in this scope
}

類對象共享靜態(tài)成員

靜態(tài)類成員有一個特點(diǎn):無論創(chuàng)建了多少個對象,程序都只創(chuàng)建一個靜態(tài)類變量副本。也就是說,類的所有對象共享同一個靜態(tài)成員。靜態(tài)數(shù)據(jù)成員和普通數(shù)據(jù)成員一樣遵從public,protected,private訪問規(guī)則;

在這里插入圖片描述

接下來看另一個代碼示例

#include <iostream>
using namespace std;
class A {
public:
    static int x;
    int y;
    static void f() {
        // y++; Error invalid use of member 'y' in static member function
        x++;
        cout << "A static function, x = " << x << endl;
    }
};
int A::x;
int main() {
    A a;
    cout << "x = " << A::x << endl;
    cout << "x = " << a.x << endl;
    A::f();
    a.f();
    return 0;
}

output

x = 0
x = 0
A static function, x = 1
A static function, x = 2

const constexpr

C++提供了多種在類中定義常量的方式,其中比較常用的有 const,constexprenum

class X
{
	// method1 const
    const static int n = 1;
    const static int m{2}; // since C++11
    const static int k; // ok
    // method2 enum
    enum {Month=12};
    // method3 constexpr
    constexpr static int arr[] = { 1, 2, 3 };        // OK
    constexpr static std::complex<double> n = {1,2}; // OK
    constexpr static int k; // Error: constexpr static requires an initializer
};
const int X::k = 3;

其中注意:

1.使用 enum 時并不會創(chuàng)建數(shù)據(jù)成員,即所有的對象中都不包括枚舉,另外Month知識一個符號名稱,在作用于為整個類的代碼中遇到它是,編譯器將用12來替代它。而且只能是整數(shù)。

2.使用 constexpr 來創(chuàng)建類常量時,一定要給其定義,不能只是聲明,而const可以只是聲明,不用給出定義。 

靜態(tài)成員函數(shù)

#include <iostream>
using namespace std;
class Person {
public:
    Person() {};
    Person(char *name, int age);
    void show();
    static int getTotal();
private:
    static int m_total;
    char *m_name;
    int m_age;
};
Person::Person(char *name, int age) : m_name(name), m_age(age) {
    m_total++;
}
void Person::show() {
    cout << m_name << "的年齡是" << m_age << ", 總?cè)藬?shù)是" << m_total << endl;
}
int Person::getTotal() {
    return m_total;
}
// 一定要先初始化
int Person::m_total = 0;
int main() {
    Person *p1 = new Person("Alice", 18);
    Person *p2 = new Person("Bob", 18);
    p1->show();
    p2->show();
    int total1 = Person::getTotal();
    int total2 = p1->getTotal();
    cout << "total1 = " << total1 << ", total2 = " << total2 << endl;
    return 0;
}

靜態(tài)成員函數(shù)與普通成員函數(shù)的根本區(qū)別在于:普通成員函數(shù)有 this 指針,可以訪問類中的任意成員;而靜態(tài)成員函數(shù)沒有 this 指針,只能訪問靜態(tài)成員(包括靜態(tài)成員變量和靜態(tài)成員函數(shù))。這里要注意的是普通的成員函數(shù)也能訪問靜態(tài)成員變量。這一點(diǎn)上和Java中的static用法很像。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!      

相關(guān)文章

  • C++中變量進(jìn)行初始化的3種方法

    C++中變量進(jìn)行初始化的3種方法

    本文主要介紹了C++中變量進(jìn)行初始化的3種方法,包括用"=",構(gòu)造函數(shù)初始化以及統(tǒng)一初始化這三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • C語言中操作字符串的函數(shù)詳解

    C語言中操作字符串的函數(shù)詳解

    這篇文章主要為大家詳細(xì)介紹了C語言中操作字符串的函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++ 中CListCtrl的每個項(xiàng)都顯示不同的提示信息

    C++ 中CListCtrl的每個項(xiàng)都顯示不同的提示信息

    這篇文章主要介紹了C++ 中CListCtrl的每個項(xiàng)都顯示不同的提示信息的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • vc中SendMessage自定義消息函數(shù)用法實(shí)例

    vc中SendMessage自定義消息函數(shù)用法實(shí)例

    這篇文章主要介紹了vc中SendMessage自定義消息函數(shù)用法,以實(shí)例實(shí)行詳細(xì)講述了SendMessage的定義、原理與用法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C語言MFC基礎(chǔ)之計(jì)算器詳解

    C語言MFC基礎(chǔ)之計(jì)算器詳解

    這篇文章主要為大家介紹了MFC實(shí)現(xiàn)簡單的計(jì)算器,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-08-08
  • C語言實(shí)現(xiàn)全排列算法模板的方法

    C語言實(shí)現(xiàn)全排列算法模板的方法

    這篇文章主要介紹了C語言實(shí)現(xiàn)全排列算法模板的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • C語言之陷阱與缺陷詳解

    C語言之陷阱與缺陷詳解

    本片文章是對C++中陷阱與缺陷進(jìn)行了詳細(xì)的分析介紹,小編覺得本片文章講解的非常詳細(xì),需要的朋友參考下,希望能夠給你帶來幫助
    2021-09-09
  • 超詳細(xì)的c語言字符串操作函數(shù)教程

    超詳細(xì)的c語言字符串操作函數(shù)教程

    字符串是一種重要的數(shù)據(jù)類型,有零個或多個字符組成的有限串行,下面這篇文章主要給大家介紹了關(guān)于c語言字符串操作函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • C++ 前置聲明詳解及實(shí)例

    C++ 前置聲明詳解及實(shí)例

    這篇文章主要介紹了C++ 前置聲明詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C語言實(shí)現(xiàn)逆序輸出詳細(xì)

    C語言實(shí)現(xiàn)逆序輸出詳細(xì)

    這篇文章主要介紹了C語言實(shí)現(xiàn)逆序輸出。主要實(shí)現(xiàn)C語言實(shí)現(xiàn)對數(shù)組元素依次賦值然后按照逆序輸出,下面文章小編將詳細(xì)解說,需要的朋友可以參考一下
    2021-10-10

最新評論

京山县| 炎陵县| 秦皇岛市| 桃园市| 高密市| 宁阳县| 甘谷县| 杭锦旗| 大埔区| 易门县| 永春县| 都兰县| 府谷县| 察哈| 称多县| 琼结县| 成武县| 南陵县| 龙川县| 阿荣旗| 文水县| 尉犁县| 红河县| 兴化市| 新野县| 民权县| 桂东县| 太康县| 夏邑县| 栾城县| 江城| 荃湾区| 庆元县| 宿迁市| 永登县| 富平县| 京山县| 临夏县| 大城县| 杭州市| 浠水县|