Opaque data type
Encyclopedia
In computer science
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...

, an opaque data type is a user defined data type
Data type
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 operations that can be done on values of that type; the meaning of the data; and the way values of...

 used like built-in data type. It is incompletely defined in an interface, so that ordinary client programs
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 can only manipulate data of that type by calling procedures
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 that have access to the missing information.

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 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
    Record (computer science)
    In computer science, a record is an instance of a product of primitive data types called a tuple. In C it is the compound data in a struct. Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed...

     (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 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 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
    Object-oriented programming
    Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

    . 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.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK