What are the differences between struct and class in C++?

后端 未结 30 3276
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

30条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 05:51

    Classes are Reference types and Structures are Values types.
    When I say Classes are reference types,
    basically they will contain the address of an instance variables.

    For example:

    Class MyClass
    {
        Public Int DataMember;  //By default, accessibility of class data members 
                                //will be private. So I am making it as Public which 
                                //can be accessed outside of the class.
    }
    

    In main method,
    I can create an instance of this class using new operator that allocates memory for this class
    and stores the base address of that into MyClass type variable(_myClassObject2).

    Static Public void Main (string[] arg)
    {
        MyClass _myClassObject1 = new MyClass();
        _myClassObject1.DataMember = 10;
    
        MyClass _myClassObject2 = _myClassObject1;
        _myClassObject2.DataMember=20;
    }
    

    In the above program, MyClass _myClassObject2 = _myClassObject1; instruction indicates that both variables of type MyClass

    1. myClassObject1
    2. myClassObject2

    and will point to the same memory location.
    It basically assigns the same memory location into another variable of same type.

    So if any changes that we make in any one of the objects type MyClass will have an effect on another
    since both are pointing to the same memory location.

    "_myClassObject1.DataMember = 10;" at this line both the object’s data members will contain the value of 10.
    "_myClassObject2.DataMember = 20;" at this line both the object’s data member will contains the value of 20.
    Eventually, we are accessing datamembers of an object through pointers.

    Unlike classes, structures are value types. For example:

    Structure MyStructure
    {
        Public Int DataMember;  //By default, accessibility of Structure data 
                                //members will be private. So I am making it as 
                                //Public which can be accessed out side of the structure.
    }
    
    Static Public void Main (string[] arg)
    {
        MyStructure _myStructObject1 = new MyStructure();
        _myStructObject1.DataMember = 10;
    
        MyStructure _myStructObject2 = _myStructObject1;
        _myStructObject2.DataMember = 20;
    }
    

    In the above program,
    instantiating the object of MyStructure type using new operator and
    storing address into _myStructObject variable of type MyStructure and
    assigning value 10 to data member of the structure using "_myStructObject1.DataMember = 10".

    In the next line,
    I am declaring another variable _myStructObject2 of type MyStructure and assigning _myStructObject1 into that.
    Here .NET C# compiler creates another copy of _myStructureObject1 object and
    assigns that memory location into MyStructure variable _myStructObject2.

    So whatever change we make on _myStructObject1 will never have an effect on another variable _myStructObject2 of type MyStructrue.
    That’s why we are saying Structures are value types.

    So the immediate Base class for class is Object and immediate Base class for Structure is ValueType which inherits from Object.
    Classes will support an Inheritance whereas Structures won’t.

    How are we saying that?
    And what is the reason behind that?
    The answer is Classes.

    It can be abstract, sealed, static, and partial and can’t be Private, Protected and protected internal.

提交回复
热议问题