首頁 > 軟體

C++中的struct和class的區別詳解

2022-08-10 18:02:09

1. C++的struct和class的區別

差異特性structclass
成員存取範圍預設public預設private
繼承關係存取範圍預設public預設private
{}初始化1、純資料或純資料+普通方法的結構體支援;2、帶建構函式或虛方法的結構體不支援不支援

1.1 成員存取範圍的差異

struct

struct Person {
    int age;
}
Person person = new Person();
person.age = 12;

可以正常的編譯和執行。

class

class Person {
    int age;
}
Person person = new Person();
person.age = 12; // 編譯出錯,應改為public int age;

【擴充套件】如果不增加public關鍵字,又想在某個類如ClassA中能存取到這個Person類中的成員,可以通過友元類(friend class Xxx)來將Person中的private和protected的成員共用出去。

Person類可以這麼編寫:

struct Person {
   	 friend class ClassA;
     int age;
}
Person person = new Person();
person.age = 12;

在ClassA中,就可以存取Person中的所有成員了。

void ClassA:setAge() {
	Person *person = new Person();
	person->age = 12;
}

關於友元類的使用,可見c++論壇:https://cplusplus.com/forum/beginner/147733/

1.1 繼承關係存取範圍的差異

指的是子物件是否可存取到父物件中的成員。

struct : struct

struct SBase {
public:
    int age = 0;
    SBase() = default;
    virtual ~SBase() = default;
};

struct Person : SBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

存取正常:

struct : class

class CBase {
public:
    int age = 0;
    CBase() = default;
    virtual ~CBase() = default;
};

struct Person : CBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

存取正常。

struct : private class

class CBase {
public:
    int age = 0;
    CBase() = default;
    virtual ~CBase() = default;
};

struct Person : private CBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

編譯錯誤:不可存取。

class : class

class CBase {
public:
    int age = 0;
    CBase() = default;
    virtual ~CBase() = default;
};

class Person : CBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

編譯錯誤:不可存取。

class : public class

class CBase {
public:
    int age = 0;
    CBase() = default;
    virtual ~CBase() = default;
};

class Person : public CBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

存取正常。

class : struct

struct SBase {
public:
    int age = 0;
    SBase() = default;
    virtual ~SBase() = default;
};

struct Person : SBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

存取正常。

class : public struct

struct SBase {
public:
    int age = 0;
    SBase() = default;
    virtual ~SBase() = default;
};

struct Person : public SBase {
public:
    Person() = default;
    virtual ~Person() = default;
};

int main(int argc, const char **argv)
{
    Person* child = new Person();
    child->age = 1;
    fprintf(stdout, "test: age=%dn", child->age);
}

存取正常。

【總結】

  • 1)子為class的,要想存取到父的public成員,需要加public關鍵字,即class: public xxx
  • 2)子為struct,可加可不加public,都能存取到父類別/結構體的成員

1.3 {}初始化的差異

struct – 純資料+一般方法

struct StructA {
    void send();
    int a;
    long b;
    string str;
};

void StructA::send()
{
    fprintf(stdout, "StructA: sending...n");
}

int main(int argc, const char **argv)
{
    StructA aS = {12, 34, "a test"};
    aS.send();
    fprintf(stdout, "StructA: a=%d, b=%ld, str=%sn", aS.a, aS.b, aS.str.c_str());
}

可直接用{}初始化資料:

struct – 帶建構函式

struct StructA {
    void send();
    int a;
    long b;
    string str;
    StructA();
};

void StructA::send()
{
    fprintf(stdout, "StructA: sending...n");
}

int main(int argc, const char **argv)
{
    StructA aS = {12, 34, "a test"};
    aS.send();
    fprintf(stdout, "StructA: a=%d, b=%ld, str=%sn", aS.a, aS.b, aS.str.c_str());
}

編譯失敗:

 struct – 帶虛方法

struct StructA {
    void virtual send();
    int a;
    long b;
    string str;
};

void StructA::send()
{
    fprintf(stdout, "StructA: sending...n");
}

int main(int argc, const char **argv)
{
    StructA aS = {12, 34, "a test"};
    aS.send();
    fprintf(stdout, "StructA: a=%d, b=%ld, str=%sn", aS.a, aS.b, aS.str.c_str());
}

編譯失敗:

class

class ClassA {
    int a;
    long b;
    string str;
};

int main(int argc, const char **argv)
{
    ClassA cA = {12, 34, "a test"};
}

編譯失敗:

2. 拓展 :C和C++的struct的區別

到此這篇關於C++中的struct和class的區別詳解的文章就介紹到這了,更多相關C++的struct與class內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com