- Opaque data type
-
In computer science, an opaque data type is a user defined data type used like built-in data type. It is incompletely defined in an interface, so that ordinary client programs can only manipulate data of that type by calling procedures that have access to the missing information.
Contents
Overview
Opaque data type can only operate on using functions or macros. It is used when the programmer does not want others to know about function parameters.
Files like <stdio.h> clients can have a FILE* at opening files with fopen(), closing files with fclose(), can read or write to file with fputs() or fgets() but one can never directly access an entry of file.
Uses in various languages
- In C, allow the declaration of opaque records (structs), whose size and fields are hidden from the client. The only thing that the client can do with an object of such a type is to take its address, to produce an opaque pointer. Opaque data type mostly refers to an incomplete struct. It is neither declared nor defined. If the information provided by the interface is sufficient to determine the type's size, then clients can declare variables, fields, and arrays of that type, assign their values, and possibly compare them for equality.
-
- Example
- struct H; everything about H is hidden. Only H& and H* can be used in some contexts.
- In Java, the only kind of opaque type provided is the opaque pointer. Indeed, in Java and several other languages records are always handled through pointers.
- Some languages allow partially opaque types, e.g. a record which has some public fields, known and accessible to all clients, and some hidden fields which are not revealed in the interface. Such types play a fundamental role in object-oriented programming. The information which is missing in the interface may be declared in its implementation, or in another friends-only interface. This second option allows the hidden information to be shared by two or more modules.
Opaque data pointer
While designing a clean API, it is important to hide as many implementations as possible. The purpose of this is to hide the structure from client which allows them to manipulate the internals of the object directly. So the object pointer given back to the client should be opaque. In that it does not expose any data members directly.
void* is one of the most simplistic ways to expose opaque data pointer. It may represent an arbitrary memory location but does not describe the format of the memory location in any way. So a pointer to a class instance or pointer to a structure instance can be passed back. The downside of using void* is all semblance of type safety are lost.
See also
References
Data types Uninterpreted Numeric - Integer
- Fixed-point
- Floating-point
- Rational
- Complex
- Bignum
- Interval
- Decimal
Text Pointer Composite Other - Boolean
- Bottom type
- Collection
- Enumerated type
- Exception
- Function type
- Opaque data type
- Recursive data type
- Semaphore
- Stream
- Top type
- Type class
- Unit type
- Void
Related topics - Abstract data type
- Data structure
- Interface
- Kind
- Primitive data type
- Subtyping
- Template
- Type constructor
- Parametric polymorphism
Categories:- Data types
- Object-oriented programming
- Computer science stubs
Wikimedia Foundation. 2010.