struct和class两个关键字的区别

1. 《C++ Primer》

用class和struct关键字定义类的唯一差别在于默认访问级别:默认情况下,struct的成员为public,而class的成员为private。

2. class关键字还可以用于定义模板类型,struct不可以。

#include<iostream>                                                                                                                                                                            
                                                                                                                                                                                              
class C0                                                                                                                                                                                      
{                                                                                                                                                                                             
    int x;                                                                                                                                                                                    
};                                                                                                                                                                                            
                                                                                                                                                                                              
struct S0                                                                                                                                                                                     
{                                                                                                                                                                                             
    int x;                                                                                                                                                                                    
};                                                                                                                                                                                            
                                                                                                                                                                                              
class C1:S0                                                                                                                                                                                   
{                                                                                                                                                                                             
    int y;                                                                                                                                                                                    
};                                                                                                                                                                                            
                                                                                                                                                                                              
struct S1:S0                                                                                                                                                                                  
{                                                                                                                                                                                             
    int y;                                                                                                                                                                                    
};                                                                                                                                                                                            
                                                                                                                                                                                              
template <struct T> //error                                                                                                                                                                          
T comp(T t0, T t1)                                                                                                                                                                            
{                                                                                                                                                                                             
    return t0-t1;                                                                                                                                                                             
}                                                                                                                                                                                             
                                                                                                                                                                                              
int main()                                                                                                                                                                                    
{                                                                                                                                                                                             
    C0 c0;                                                                                                                                                                                    
    //c0.x = 0; // error                                                                                                                                                                               
    C1 c1;                                                                                                                                                                                    
    //c1.x = 0; // error                                                                                                                                                                             
                                                                                                                                                                                              
    S0 s0;                                                                                                                                                                                    
    s0.x = 0;                                                                                                                                                                                 
                                                                                                                                                                                              
    S1 s1;                                                                                                                                                                                    
    s1.x = 0;                                                                                                                                                                                 
                                                                                                                                                                                              
    std::cout << comp(1,2);                                                                                                                                                                   
} 
原文地址:https://www.cnblogs.com/keviwu/p/6063153.html