Union in c++ are they feasible

前端 未结 5 1897
死守一世寂寞
死守一世寂寞 2021-02-07 01:45

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 an

5条回答
  •  你的背包
    2021-02-07 02:10

    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.

提交回复
热议问题