Opaque data type

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


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Data type — For other uses, see Data type (disambiguation). In computer programming, a data type is a classification identifying one of various types of data, such as floating point, integer, or Boolean, that determines the possible values for that type; the …   Wikipedia

  • Algebraic data type — In computer programming, particularly functional programming and type theory, an algebraic data type (sometimes also called a variant type[1]) is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of… …   Wikipedia

  • Array data type — Not to be confused with Array data structure. In computer science, an array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices that can be computed at run time by the …   Wikipedia

  • Complex data type — Some programming languages provide a complex data type for complex number storage and arithmetic as a built in (primitive) data type. In some programming environments the term complex data type (in contrast to primitive data types) is a synonym… …   Wikipedia

  • Composite data type — In computer science, a composite data type is any data type which can be constructed in a program using its programming language s primitive data types and other composite types. The act of constructing a composite type is known as composition.… …   Wikipedia

  • Decimal data type — Some programming languages provide a built in (primitive) or library decimal data type to represent non repeating decimal fractions like 0.3 and 1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal type of Python …   Wikipedia

  • Opaque pointer — In computer programming, an opaque pointer is a special case of an opaque data type, a datatype that is declared to be a pointer to a record or data structure of some unspecified type. Opaque pointers are present in several programming languages… …   Wikipedia

  • Data structure — In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.[1][2] Different kinds of data structures are suited to different kinds of applications, and some are highly …   Wikipedia

  • Container (data structure) — For the abstract notion of containers in Type theory, see Container (Type theory). In computer science, a container is a class, a data structure[1][2], or an abstract data type (ADT) whose instances are collections of other objects. In other… …   Wikipedia

  • Option type — For families of option contracts in finance, see Option style. In programming languages (especially functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”