加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

结构和类在C中是否真的相同?

发布时间:2020-12-16 10:37:55 所属栏目:百科 来源:网络整理
导读:假设结构与类相似,除了默认的成员隐私,为什么下面的代码不能编译? #define class struct#include iostreamint main(){ return 0;} 更新 In file included from /usr/include/c++/7/bits/stl_algobase.h:61:0,from /usr/include/c++/7/vector:60,from main.c
假设结构与类相似,除了默认的成员隐私,为什么下面的代码不能编译?

#define class struct
#include <iostream>


int main()
{
     return 0;
}

更新

In file included from /usr/include/c++/7/bits/stl_algobase.h:61:0,from /usr/include/c++/7/vector:60,from main.cpp:5:
/usr/include/c++/7/bits/cpp_type_traits.h:86:18: error: ‘struct std::_Sp’ is not a valid type for a template non-type parameter
   template<class _Sp,class _Tp>
                  ^~~
compilation terminated due to -Wfatal-errors.

解决方法

类和结构是等价的(默认隐私除外)

但语法不允许在模板中使用struct:

template <struct S> // Invalid
 /*..*/

template <class C> // valid
 /*..*/

要么

template <typename T> // valid
 /*..*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读