In depth analysis of the difference between struct and class in C++

  • 2020-04-02 00:53:04
  • OfStack

A,
Struct in C++ extends struct in C, it is no longer just a data structure containing different data types, it has acquired too much functionality.
Can structs contain member functions?     Can!!!!
Can structs inherit?   Can!!!!!
Can struct be polymorphic?     Can!!!!!!!!!
 
One essential difference is the default access control in two ways:
1) default inherited access. Struct is public, class is private.
    Write the following code:

struct A
{
  char a;
} ; 
struct B : A
{
  char b;
} ; 

At this point B is inherited from A by public. If you change the struct above to class, then B is private inherited from A. This is the default inherited access. So when we write class inheritance, we usually say:
Struct B: public A
This is to indicate that it is public inheritance, not the default private inheritance.
Of course, whether the default is public or private depends on the subclass, not the base class. I mean, a struct can inherit from a class, and a class can inherit from a struct, so the default inheritance access is depending on whether the subclass USES a struct or a class. As follows:
Struct A {};
Class B: A {}; / / private inheritance
Struct C: B{};   / / public inheritance
 
2) as the implementation body of the data structure, struct's default data access control is public, while class, as the implementation body of the object, its default member variable access control is private.
3) the "class" keyword is also used to define template parameters, like "typename". But the keyword "struct" is not used to define template parameters.

4) as mentioned above, struct in C++ is an expansion of struct in C. Since it is an expansion, it should be compatible with all the features of struct in past C. For example, you could write:
Struct A// defines A struct
{
  Char c1;
  int   N2.
  Double db3;
};
A. A = {' p ', 7,3.1415926};   // is directly assigned when defined
That is, a struct can be defined with an initial value of {}.
A constructor (or virtual function) is added to the struct above, and the struct cannot be initially evaluated with {}. True, the initial value is assigned in the form of {}, but the data is initialized sequentially with an initialization list, as shown above if A A ={'p',7}; So c1,n2 is initialized, but db3 is not. This simple copy operation can only occur on simple data structures and should not be placed on objects. Adding a constructor or a virtual function makes the struct more object-specific and makes the {} operation no longer valid. In fact, the internal structure of the class is changed by the addition of such a function. What about adding a regular member function? You will find that {} is still available. You can think of ordinary functions as algorithms for data structures without breaking the nature of their data structures. As for the difference between virtual functions and ordinary member functions, I will write a specific article to discuss.

So, looking at this, we see that even if a struct wants to use {} to assign an initial value, it has to satisfy a lot of constraints that actually make a struct more of a data mechanism than a class. So why can't we just change the struct up here to class, {}? The problem happens to be what we talked about earlier - access control! Look, what have we forgotten? Yes, when you change the struct to class, the access control changes from public to private, so of course you can't use {} to assign an initial value. Add a public, you will find that class can also use {}, and struct is no different!!
From the above differences, we can see that struct is more suitable for the implementation of a data structure, and class is more suitable for the implementation of an object.

Second,
About using braces to initialize
Neither class nor struct can be initialized with braces if the constructor is defined
If a constructor is not defined, a struct can be initialized with braces.
If a constructor is not defined and all member variables are public, you can initialize with braces.
About default access
The default member access in class is private, while in struct it is public.
About inheritance
Class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance.
Look at the following code (see the error message given by the compiler) :

class T1
{
 public:
  void f()
  {
   cout<<"T1::f()"<<endl;
  }
 int x,y;
};
struct T2
{
 int x;
 void f(){cout<<"T2::f()"<<endl;}
};
struct TT1 : T1
{
};
class TT2 : T2
{
};
int main()
{
 TT1 t1;
 TT2 t2;
 t1.f();
 t2.f();
}

About template
In a template, a type parameter can be preceded by a class or typename, but a struct has a different meaning. A struct is followed by a "non-type template parameter," while a class or typename is followed by a type parameter.
The template < Struct X >
Void f (X) X
{
}
/ / error: d: codecpptestcpptestcpptest. CPP (33) : error C2065: 'X' : undeclared identifier

Related articles: