zoukankan      html  css  js  c++  java
  • C和C++结构体的区别

    1. C的结构体内不允许有函数存在,C++允许有内部成员函数,且允许该函数是虚函数。所以C的结构体是没有构造函数、析构函数、和this指针的。
    2. C的结构体对内部成员变量的访问权限只能是public,而C++允许public,protected,private三种。
    3. C语言的结构体是不可以继承的,C++的结构体是可以从其他的结构体或者类继承过来的。
    4. 在C中定义一个结构体类型要用typedef,如下:
      typedef struct Complex{
        int read;
        int image;
      }Complex;

      那么,在说明Complex变量的时候可以这样写

      Complex complex;

      但是如果没有typedef就必须用

      struct Complex complex;

      来声明。这里的Complex实际上就是struct Complex的别名。另外这里也可以不写Complex(于是也不能struct Complex complex;了)

      typedef struct{
        int read;
        int image;
      }Complex;

      但在c++里很简单,直接

      struct Complex{
        int read;
        int image;
      };

      于是就定义了结构体类型Complex,声明变量时直接Complex complex;

    5. 在c++中如果用typedef的话,又会造成区别:
      struct Complex1{
        int read;
        int image;
      }complex;//complex是一个变量
       
      typedef   struct   Complex2{
        int read;
        int image;
      }Complex2;//Complex是一个结构体类型

      使用时可以直接访问complex.read,但是Complex2则必须先Complex2 complex2;然后complex2.read = 1;

  • 相关阅读:
    97. Interleaving String
    96. Unique Binary Search Trees
    95. Unique Binary Search Trees II
    94. Binary Tree Inorder Traversal
    odoo many2many字段 指定打开的form视图
    docker sentry 配置文件位置
    postgres 计算时差
    postgres 字符操作补位,字符切割
    postgres判断字符串是否为时间,数字
    odoo fields_view_get
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/7814970.html
Copyright © 2011-2022 走看看