[英]Constructor, Class, union, struct
I have some doubts I would like to clear regarding class, struct and union. 关于类,结构和联合,我有一些疑问要清除。 I know class and struct are considered object.
我知道class和struct被认为是对象。 I would like to check whether union is consider object too.
我想检查工会是否也被视为对象。 Whereby, I can have a constructor inside?
借此,我可以在内部构造一个构造函数?
union ABC
{
ABC() {};
int x;
}A, B, C
Since I have object name. 由于我有对象名称。 I could do this?
我能做到吗?
Union ABC obj1(); //Call constructor?
A.x; //Variable accessing the member
??? ??? Secondly, what if i typedef union.
其次,如果我输入typedef union怎么办。 What does ABC mean?
ABC是什么意思? Can I do this
我可以这样做吗
union ABC obj1();
A.x;
typedef union ABC
{
ABC() {};
int x;
}A, B, C
The difference between a struct and a class is that by default members in struct are public, but private in class; 结构和类之间的区别在于,默认情况下,结构中的成员是公共的,但在类中是私有的; otherwise, they are functional equivalent.
否则,它们在功能上是等效的。 A union can have member functions (including constructors and destructors), but not virtual functions.
联合可以具有成员函数(包括构造函数和析构函数),但不能具有虚函数。 A union shall not have base classes.
工会不得有基层阶级。 A union shall not be used as a base class.
联合不得用作基类。
Union ABC obj1(); //Call constructor?
No, This does not call a constructor 不,这不会调用构造函数
union ABC obj1()
means you declare a function with name obj1
and returns an object of union ABC
union ABC obj1()
表示您声明名称为obj1
的函数并返回obj1
union ABC
的对象
You have syntax errors in both cases, you have to do the following: 两种情况下都存在语法错误,因此必须执行以下操作:
union ABC { ABC() {}; int x; }A, B, C; //You cannot miss this ;
You can do: 你可以做:
typedef union ABC ABCUnion; then ABCUnion A, B,C;
It is the same as 与...相同
union ABC A, B,C;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.