C++ Standard has an explanation for the difference. The first difference is described in 11.2 Accessibility of base classes and base class members
In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.
The second difference is described in 11.1 Access specifiers. The explanation is in comments in the example code.
The following code shows this difference.
#include <iostream>
using namespace std;
struct SPerson {
void hello() const {
cout << "hello\n";
}
};
class CPerson {
void hello() const {
cout << "hello\n";
}
};
class Base {
public:
void hello() const {
cout << "hello\n";
}
};
struct SDerived: Base {
};
class CDerived: Base {
};
int main(int argc, const char *argv[]) {
SPerson sp;
sp.hello();
CPerson cp;
cp.hello(); // compile error
SDerived sd;
sd.hello();
CDerived cd;
cd.hello(); // compile error
return 0;
}
http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c is a discussion about the difference. Since stackoverflow can't be accessed sometimes in China, I write this post.
分享到:
相关推荐
在网络通讯过程中往往涉及一些有关联的参数传递,例如结构体之类的。对于结构体其实方法挺简单,由于结构体对象在内存中分配的空间都是连续的,所以可以将整个结构体直接转化成字符串发送,到了接收方再将这个字符串...
c++笔记struct和typedef struct彻底明白了
C_C++中typedef_struct和struct的用法。
详细描述了class与struct本质区别
今天这篇博文主要讲解在C++中关键字struct和class的区别。这篇博文,将会系统的将这两个关键字的不同面进行详细的讲解。 从语法上来讲,class和struct做类型定义时只有两点区别: 1.默认继承权限,如果不指定,来自...
Swift-Class-Struct Swift-Class-Struct
C++和c语言的混用已经司空见惯,但其中还不乏一些细节应该注意,用错会导致一些意外发生;本文就c语言环境的struct用在c++后,错误加virtual关键字导致崩溃详述
Transformation between JSON and struct. (类似Java Gson,C语言实现.zip
C++结构体struct介绍及应用,struct声明,初始化,应用
C++封装(class,struct,public,protected,private,friend)的博客例程。 https://blog.csdn.net/flysnow010/article/details/136907034
主要讲述c/c++中struct和typedef struct的区别以及各种用法
从语法上,在C++中(只讨论C++中)。class和struct做类型定义时... 不能因为学过C就总觉得连C++中struct和class都区别很大,下面列举的说明可能比较无聊,因为struct和class本来就是基本一样的东西,无需多说。但这些
2、C++中的 struct 和 class 有什么区别? 【参考答案】从语法上讲,class和struct做类型定义时只有两点区别: (一)默认继承权限。如果不明确指定,来自class的继承按照private继承处理,来自struct的继承按照...
hash_set c++总结(自定义类型stuct、class)。总结自定义struct、class三个案例。find函数测试,hash_set迭代器。
从语法上,在C++中(只讨论C++中)。class和struct做类型定义时... 不能因为学过C就总觉得连C++中struct和class都区别很大,下面列举的说明可能比较无聊,因为struct和class本来就是基本一样的东西,无需多说。但这些
类转换为结构体的方法,C++的类到C代码的移植
本文主要谈了一下 C语言中class和struct的区别。
本篇文章是对C++中struct与class的区别进行了详细的分析介绍,需要的朋友参考下
本程序中定义了一个结构体,其成员有一个union,union中又定义了一个结构体,很好的体现了使用union的方法