在c ++中的联合是可行的 [英] Union in c++ are they feasible

查看:125
本文介绍了在c ++中的联合是可行的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的联合可以有成员函数吗?如果创建对象,如何存在数据成员和成员函数的联合?



如果我假设是,那么它们是否可行。如果是,那么在哪里?

解决方案

9.5 / 1


联合可以有成员函数
(包括构造函数和
析构函数),但不能是虚函数(10.3)
函数。联合不得有
类。联合不能用作
基类。具有非平凡构造函数(12.1),
a非平凡复制构造函数(12.8),
a非平凡析构函数(12.4)或
的类
的对象非平凡复制赋值运算符
(13.5.3,12.8)不能是
联合的成员,也不能是这样的
对象的数组


< block是什么意思? 如果创建对象,如何存在数据成员和成员函数?成员函数(非虚拟)任何类/联合的实例中的空格。


Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created?

If I suppose yes, then are they feasible any where. If yes then where?

解决方案

9.5/1

A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor can an array of such objects

What do you mean by How do union with data members and member functions exist if an object is created? Member functions (non-virtual) take no space in an instance of any class/union.

这篇关于在c ++中的联合是可行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆