C和C++的区别(2)结构体
一,成员
1,C++允许有内部成员函数,且允许该函数是虚函数,C的结构体内不允许有函数存在,但是可以有函数指针。所以C的结构体是没有构造函数、析构函数、和this指针的。
2,C的结构体对内部成员变量的访问权限只能是public,而C++允许public,protected,private三种。
二,继承
1,C语言的结构体是不可以继承的,C++的结构体是可以从其他的结构体或者类继承过来的。
三,定义方式
1,C语言定义结构体
(1)不带typedef
一种创建实例的写法:
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      };
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      struct A a;
     
    
 (2)先定义结构体,再typedef
同名定义,两种创建实例的写法:
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      };
     
    
- 
    
     
    
    
     
      typedef struct A A;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      struct A a;
     
    
- 
    
     
    
    
     
      A a2;
     
    
 不同名定义,两种创建实例的写法:
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      };
     
    
- 
    
     
    
    
     
      typedef struct A B;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      struct A a;
     
    
- 
    
     
    
    
     
      B a2;
     
    
 (3)typedef 加 结构体定义
同名定义,两种创建实例的写法:
  
   - 
    
     
    
    
     
      typedef struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      }A;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      struct A a;
     
    
- 
    
     
    
    
     
      A a2;
     
    
 不同名定义,两种创建实例的写法:
  
   - 
    
     
    
    
     
      typedef struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      }B;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      struct A a;
     
    
- 
    
     
    
    
     
      B a2;
     
    
 本质上,先定义结构体再typedef,和typedef 加 结构体定义,并没有什么区别。
(4)typedef匿名结构体
一种创建实例的写法:
  
   - 
    
     
    
    
     
      typedef struct{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      }A;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      A a;
     
    
 2,C++定义结构体
C++除了可以使用上述C语言能使用的所有写法之外,还有3种额外写法:
(1)不带typedef
struct可以省略
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      };
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      A a;
     
    
 (2)结构体定义并创建实例,不同名
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      }a;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int main()
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
         struct A b;
     
    
- 
    
     
    
    
     
          A c;
     
    
- 
    
     
    
    
     
          a.x=12345,b.x=1234,c.x=123;
     
    
- 
    
     
    
    
     
          cout<<a.x<<" "<<b.x<<" "<<c.x;
     
    
- 
    
     
    
    
         return 0;
     
    
- 
    
     
    
    
     
      }
     
    
 输出:12345 1234 123
(3)结构体定义并创建实例,同名
  
   - 
    
     
    
    
     
      struct A{
     
    
- 
    
     
    
    
         int x;
     
    
- 
    
     
    
    
     
      }A;
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      int main()
     
    
- 
    
     
    
    
     
      {
     
    
- 
    
     
    
    
         struct A a;
     
    
- 
    
     
    
    
     
          a.x=12345,A.x=123;
     
    
- 
    
     
    
    
     
          cout<<a.x<<" "<<A.x;
     
    
- 
    
     
    
    
         return 0;
     
    
- 
    
     
    
    
     
      }
     
    
 输出:12345 123
文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/nameofcsdn/article/details/109289414
- 点赞
- 收藏
- 关注作者
 
             
           
评论(0)