《C++那些事》項目概要及一文徹底搞懂C和C++中struct

  • 2019 年 10 月 6 日
  • 筆記

一文搞懂C和C++中struct

最近一直在更新一個倉庫:《C++那些事》,將自己學習的難點與重點羅列進去,並配上相關程式碼,實踐與理論結合。

倉庫地址:

https://github.com/Light-City/CPlusPlusThings

點擊原文也可跳轉,如果覺得不錯,麻煩點個star!

目前C++那些事更新文章如下:

1.C中struct

  • 在C中struct只單純的用作數據的複合類型,也就是說,在結構體聲明中只能將數據成員放在裡面,而不能將函數放在裡面。
  • 在C結構體聲明中不能使用C++訪問修飾符,如:public、protected、private 而在C++中可以使用。
  • 在C中定義結構體變數,如果使用了下面定義必須加struct。
  • C的結構體不能繼承(沒有這一概念)。
  • 若結構體的名字與函數名相同,可以正常運行且正常的調用!例如:可以定義與 struct Base 不衝突的 void Base() {}。

完整案例:

#include<stdio.h>    struct Base {            // public      int v1;  //    public:      //error          int v2;      //private:          int v3;      //void print(){       // c中不能在結構體中嵌入函數      //    printf("%sn","hello world");      //};    //error!  };    void Base(){      printf("%sn","I am Base func");  }  //struct Base base1;  //ok  //Base base2; //error  int main() {      struct Base base;      base.v1=1;      //base.print();      printf("%dn",base.v1);      Base();      return 0;  }  

最後輸出:

1  I am Base func  

完整程式碼見:github

2.C++中struct

與C對比如下:

  • C++結構體中不僅可以定義數據,還可以定義函數。
  • C++結構體中可以使用訪問修飾符,如:public、protected、private 。
  • C++結構體使用可以直接使用不帶struct。
  • C++繼承
  • 若結構體的名字與函數名相同,可以正常運行且正常的調用!但是定義結構體變數時候只用用帶struct的!

例如:

情形1:不適用typedef定義結構體別名

未添加同名函數前:

struct Student {    };  Student(){}  Struct Student s; //ok  Srudent s;  //ok  

添加同名函數後:

struct Student {    };  Student(){}  Struct Student s; //ok  Srudent s;  //error  

情形二:使用typedef定義結構體別名

typedef struct Base1 {      int v1;  //    private:   //error!          int v3;      public:     //顯示聲明public          int v2;      void print(){          printf("%sn","hello world");      };  }B;  //void B() {}  //error! 符號 "B" 已經被定義為一個 "struct Base1" 的別名  

前三種案例

#include<iostream>  #include<stdio.h>    struct Base {      int v1;  //    private:   //error!          int v3;      public:     //顯示聲明public          int v2;      void print(){          printf("%sn","hello world");      };  };    int main() {      struct Base base1;  //ok      Base base2; //ok      Base base;      base.v1=1;      base.v3=2;      base.print();      printf("%dn",base.v1);      printf("%dn",base.v3);      return 0;  }  

完整程式碼見:github

繼承案例

#include<iostream>  #include<stdio.h>  struct Base {      int v1;  //    private:   //error!          int v3;      public:   //顯示聲明public          int v2;      virtual void print(){          printf("%sn","Base");      };  };  struct Derived:Base {        public:          int v2;      void print(){          printf("%sn","Derived");      };  };  int main() {      Base *b=new Derived();      b->print();      return 0;  }  

完整程式碼見:github

同名函數

#include<iostream>  #include<stdio.h>    struct Base {      int v1;  //    private:   //error!          int v3;      public:     //顯示聲明public          int v2;      void print(){          printf("%sn","hello world");      };  };    typedef struct Base1 {      int v1;  //    private:   //error!          int v3;      public:     //顯示聲明public          int v2;      void print(){          printf("%sn","hello world");      };  }B;  void Base(){      printf("%sn","I am Base func");  }  //void B() {}  //error! 符號 "B" 已經被定義為一個 "struct Base1" 的別名  int main() {      struct Base base;  //ok      //Base base1;  // error!      base.v1=1;      base.v3=2;      base.print();      printf("%dn",base.v1);      printf("%dn",base.v3);      Base();      return 0;  }  

完整程式碼見:github

3.總結

C和C++中的Struct區別

C

C++

不能將函數放在結構體聲明

能將函數放在結構體聲明

在C結構體聲明中不能使用C++訪問修飾符。

public、protected、private 在C++中可以使用。

在C中定義結構體變數,如果使用了下面定義必須加struct。

可以不加struct

結構體不能繼承(沒有這一概念)。

可以繼承

若結構體的名字與函數名相同,可以正常運行且正常的調用!

若結構體的名字與函數名相同,使用結構體,只能使用帶struct定義!