zoukankan      html  css  js  c++  java
  • C#与C++中struct和class的小结

        在C#中,struct其实也是可以像class一样封装方法和数据的。请参考如下代码。

     1 using System;
     2 
     3 namespace testDiffInStructClass
     4 {
     5     public struct father
     6     {
     7         //private string _name = "father";
     8         //private int _age = 100;
     9         private string _name;
    10         private int _age;
    11 
    12         //public father()
    13         //{
    14         //    Console.WriteLine("default ctor is called");
    15         //}
    16         public father(string name, int age)
    17         {
    18             _name = name;
    19             _age = age;
    20             Console.WriteLine("ctor with param is called");
    21         }
    22         public override string ToString()
    23         {
    24             return string.Format("father's name is: " + _name + " and of age: " + _age);
    25         }
    26     }
    27 
    28     //public struct son: public father
    29     //{
    30     //}
    31 
    32     class Program
    33     {
    34         static void Main(string[] args)
    35         {
    36             father f = new father("Neo", 25);
    37             Console.WriteLine(f.ToString());
    38         }
    39     }
    40 }

          但是有几点需要注意,即是上面注释的内容:

    1.结构中不能有实例字段初始值;

    2.结构不能包含显式的无参数构造函数;

    3.结构不能继承。

     

          但是在C++中则表现出不一样的性质。C++中,struct和class其实并没有太大差别。在大多数情况下,是可以换用的;但是存在一些区别,具体介绍如下。

    1.字面上的区别

    在字面上struct是structure的缩写,通常叫做“结构体”,在C语言里用于将多种数据、多个变量组织在一起,便于表达比较复杂的数据类型,在C++中为了兼容C语言保留了该关键字,并且保留了C语言中的所有功能。

    而class,则称作“类”,是C++新增来支持面向对象思想概念中“类”的概念的一个关键词,并且比struct具有了更强大的功能,不仅可以像C语言中的struct一样把数据组织在一起,还可以将与数据相关的方法组织在一起,并增加了如虚函数、继承等特性来支持面向对象编程。

    虽然在字面上struct与class的含义不一样,但在C++中其功能基本是相同的,C++中的struct不仅可以包含数据成员,而且与class一样支持新增的面向对象特性,仅在以下细节上有略微差别。

    既然两者在字面上不一样,为了更好地利用这一点,建议在C++中使用struct时仍然只使用C中的特性,来表示一些复杂的数据而不进行方法的封装,这样还可以提高软件的可读性。

    以下的区别的介绍,请对照参考代码。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 struct SC
     5 {
     6     void func(){}
     7 };
     8 
     9 struct SB:SC
    10 {
    11 };
    12 
    13 class CA
    14 {
    15     void func(){}
    16 };
    17 
    18 class CB:CA
    19 {
    20 };
    21 
    22 int main()
    23 {
    24     // test the default access
    25     SC sc;
    26     sc.func();// no error
    27     CA ca;
    28     ca.func();// error C2248: “CA::func”: 无法访问 private 成员(在“CA”类中声明)
    29     // test the inherit
    30     SB sb;
    31     sb.func();// no error
    32     CB cb;
    33     cb.func(); // error C2248: “CA::func”: 无法访问 private 成员(在“CA”类中声明)
    34 }

    2.默认成员权限区别

    struct的成员默认权限是public,而class的成员默认权限是private。

    3. 默认继承方式

    struct的默认继承方式为public,而class的默认继承为private。

    4. 用于定义模板参数

    模板为C++语言新增特性,C语言没有,只有class可用于定义参数,而struct不可以,例如:

     1 template
     2 class TValue {
     3 private: 
     4     T _v;
     5 public: 
     6     TValue(T v) : _v(v){}
     7     T Get( void )
     8     {
     9         return _v;
    10     }
    11 };

          此处只能使用class,不能使用struct。当然,此处还可以使用typename代替class,class与typename也仅在定义模板参数时可以互换,而且建议此时使用typename,因为这样读起来更接近人类语言,更具有可读性。

    参考文章:

    http://blog.csdn.net/nocky/article/details/6195556

    http://wenwen.sogou.com/z/q363064420.htm

  • 相关阅读:
    JEECG开发总结
    ehcache集群的配置
    spring拦截器
    spring的基本配置
    kindeditor编辑器
    jQuery中的Ajax
    表单验证
    Python中的Random模块
    Open vSwitch FAQ (二)
    Open vSwitch FAQ (一)
  • 原文地址:https://www.cnblogs.com/warnet/p/4005673.html
Copyright © 2011-2022 走看看