Use of the Union in the PLO

Unions can be used as a type, as a class and structure (with some restrictions). It may have member functions. It can be used as an OOP design.

As I understand it, unions were simply imported into C ++ to maintain backward compatibility with C.
For all these years of programming, I never used union, as I would use a class or structure as a construction of OOP.

Is there any practical use of the Union as an OOP construct (and not just as a data type) or is it just some rudimentary part of the language that is never useful?


EDIT:
The standard definitely allows the union to act as an organization of the PLO. It allows member functions in unions. The following code compiles and works and conforms to the standard:

union A { int a; int k; void doSomething(){} }; int main() { A obj; int i = obj.a; obj.doSomething(); return 0; } 

Why does the standard allow member functions in a union if it should not act as an OOP construct?
Please write the answers with specific arguments and please refrain from posting the answer. I donโ€™t think so, without good reason why?

+4
source share
6 answers

No , combining is not an OO construct.

The single most important feature of OO is polymorphism, but unions cannot participate in inheritance relations (cannot be a base of another type, cannot have a base) or have virtual functions. The only purpose of the union is to provide a data type, not an object.

+4
source

My personal opinion is no.

Historically, C has allowed developers low-level access to the host system, providing memory access in very flexible ways. By providing a design to handle the same memory area as different types of data, this flexibility has been made even simpler.

But it is difficult to understand how this can be classified as an object-oriented construction of any type.

+1
source

I think not .

union has nothing to do with the orientation of the object .

C ++ never says that it intends to support only OO. C ++ supports a multiple paradigm from the beginning (meanwhile: procedural, functional, generative, object-oriented)

C ++ 11 supports โ€œ unlimited joins, โ€ but I don't see any use in terms of OO

Depending on the author, the orientation of the object requires the following problems:

  • identity
  • inheritance / generalization
  • polymorphy
  • Encapsulation

In the case of union, the first problem is valid. The association can be identified (for example, at its address)

For unions there is no concept of inheritance .

It can be argued that the data are polymorphic in the union, but this interpretation is far from what the oo considers to be polymorphic. A union cannot have a virtual function that would be almost useless without inheritance. And that will break the memory layout.

In a combination of encapsulation . However, due to limitations, I do not expect useful applications for this case. If I were grateful for the link

With this feature set, I would consider the "abstract data type" (ADA) as the right term.

+1
source

This is not a good practice, but suppose you can make a strong guarantee that only one field will be active:

 class multi_type { private: bool unit_is_float; union { float float_member; int int_member; }; public: multi_type(bool is_float) : unit_is_float(is_float) {} bool isFloat() { return unit_is_float; } bool isInt() { return !unit_is_float; } float& getFloat() { if (!isFloat()) throw std::logic_error("not a float"); return float_member; } int& getInt() { if (!isInt()) throw std::logic_error("not an int"); return int_member; } }; 

Formatted as compressed, not very beautiful.

A valid application can be in some sort of parsing library, where the language to be analyzed can have different types of tokens that can only be determined at runtime. This is an alternative to using void * s.

+1
source

From Microsoft Help :

C ++ union is a limited form of class type. It may contain access qualifiers (public, protected, private), member data and member functions, including constructors and destructors. It cannot contain virtual functions or static data. It cannot be used as the base of a class, nor can it have base classes. The default access to members in the union is publicly available.

It is even possible to combine templates (see, for example, Templates - The Complete Guide ). In this sense, unions provide an almost identical degree of encapsulation .

Despite all these syntactic restrictions, unions fill a small hole with methods of breaking a system like C ++. Consider these consistent correspondences:

 class/struct with virtual functions <--> dynamic_cast class/struct without virtual functions <--> static_cast union <--> reinterpret_cast 

Polymorphism is to safely and controlledly violate the type system. Classes and structures break the type system in time . Using virtual functions, we can split the type system at run time; without virtual functions, we can do this at compile time (especially with templates). On the other hand, unions can break up the type system in place . Note that polymorphism and encapsulation go hand in hand: everything that can be changed is hidden from the user (either through vtables, either during compilation search, or by reinterpreting data).

Is this enough to designate unions as part of the OO programming style seems to me a matter of taste.

+1
source

As a low-level structure, union is useful when your data has different interpretations and types based on context. In this sense, it helps you keep your objects smaller than if you saved different fields that cannot exist at the same time.

Is this a serious problem for OOP? Kind, I think ...

-

One of the hackers they work with is accessing different bytes with different names (typical examples are combining four bytes of r, g, b, a and one 32-bit integer color), but this is potentially dangerous fooobar.com/questions / 1034425 / ....

0
source

Source: https://habr.com/ru/post/1410875/


All Articles