Question:

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 differences as well as reasons for choosing one or the other in OO design.

I'll start with an obvious difference:

  • If you don't specify public: or private:,
    members of a struct are public by default; members of a class are private by default.

I'm sure there are other differences to be found in the obscure corners of the C++ specification.

Answer:

You forget the tricky 2nd difference between classes and structs.

Quoth the standard (11.2.2):

In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and
private is assumed when the class is declared class.

And just for completeness' sake, the more widely known difference between class and struct is defined in (11.2):

Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by
default.

What are the differences between struct and class in C++?的更多相关文章

  1. 【GoLang】50 个 Go 开发者常犯的错误

    1. { 换行:   Opening Brace Can't Be Placed on a Separate Line 2. 定义未使用的变量:  Unused Variables 2. import ...

  2. What are the differences between a pointer variable and a reference variable in C++?

    Question: I know references are syntactic sugar, so code is easier to read and write. But what are t ...

  3. Differences between Stack and Heap

    本文转载自Differences between Stack and Heap Stack vs Heap So far we have seen how to declare basic type ...

  4. 使用struct处理二进制

    有的时候需要用python处理二进制数据,比如,存取文件.socket操作时.这时候,可以使用python的struct模块来完成. struct模块中最重要的三个函数是pack(), unpack( ...

  5. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  6. go-使用 unsafe 修改 struct 中的 field 的值

    以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...

  7. C语言中struct位域的定义和使用

    位域的定义和使用 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又 ...

  8. C# Struct结构体里数组长度的指定

    typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...

  9. C 语言Struct 实现运行类型识别 RTTI

    通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型.    (2)dynamic_cast: ...

随机推荐

  1. html-minifier中文文档

    HTMLMinifier是一个高度可配置的.经过良好测试的.基于javascript的HTML缩小器.参见相应的博客文章,了解它的工作原理.每个选项的描述.测试结果和结论.在线测试套件.还可以看到相应 ...

  2. _ZNote_Qt_Tips_添加动态链接库

    之前添加都是 手写添加,今天陈老师提示可以在 .pro 文件内空白处,右键弹出添加

  3. PMP:7.项目成本管理

    内容中包含 base64string 图片造成字符过多,拒绝显示

  4. 深入浅出TCP/IP协议

    目录 什么是网络协议? 谁来制定这个网络协议? TCI/IP协议 什么是socket? http协议属于应用层还是传输层? soap可以使用HTTP协议进行传输吗? 各层协议举例 什么是网络协议? 话 ...

  5. 第十八节:详解Java抽象类和接口的区别

    前言 对于面向对象编程来说,抽象是它的特征之一. 在Java中,实现抽象的机制分两种,一为抽象类,二为接口. 抽象类为abstract class,接口为Interface. 今天来学习一下Java中 ...

  6. 通过用jQuery写一个页面,我学到了什么

    概述 前几天面试,hr发来一个测试文件,让我做做看.我一看,其实就是根据PSD需求写一个页面,再加上一些互动效果即可. 刚好我之前学了切图,jquery等知识还没练手过,于是高兴的答应了. 最后花了3 ...

  7. Win10手记-为应用集成SQLite(二)

    接上篇内容,这里给大家分享我的辅助访问类,采用了异步方法,封装了常用的访问操作,一些操作还是纯CLI的. SQLiteDBManager using System; using System.Coll ...

  8. postgresql-分页重复数据探索

    # postgresql-分页重复数据探索 ## 问题背景 许多开发和测试人员都可能遇到过列表的数据翻下一页的时候显示了上一页的数据,也就是翻页会有重复的数据. ### 如何处理? 这个问题出现的原因 ...

  9. Ubuntu 16.04下如何安装VMware-Workstation

    一.下载 下载地址:https://my.vmware.com/cn/group/vmware/details?downloadGroup=WKST-1411-LX&productId=686 ...

  10. Xshell连接ESXI方法

    第一步.ESXI打开ssh功能按住F2进入设置如下图: 第二步.输入密码 第三步.选择Troubleshooting Options 回车 第四步.选择Enable SSH 这里只介绍了一种方式打开E ...