What exactly is an opaque pointer?
Opaque, as the name implies, is something that we cannot see through. Wood, for example, is opaque. An opaque pointer is one that points to a data structure whose contents are not known at the time it is defined.
The pointer after it is opaque. The definition does not reveal the data contained in the STest structure.
It is safe to set an opaque pointer to NULL.
Why Opaque pointer?
There are times when we just want to tell the compiler, "Hey! This is a data structure that our clients will use. Don't worry, clients will implement it while preparing the compilation unit." When dealing with shared code, this type of design is robust. Please see the following example:
Assume we're developing an image-processing app. We want to develop apps for the Windows, Android, and Apple platforms because we live in a world where everything is moving to the cloud and devices are very affordable to buy. So it would be nice to have a good design that is robust, scalable, and flexible enough to meet our needs. We could have shared code that is used by all platforms, and then different end-points could have platform-specific code.
To deal with images, we have a CImage class that exposes APIs for various image operations (scale, rotate, move, save etc).
Because all platforms will provide the same operations, we will define this class in a header file. However, the way an image is handled may differ between platforms. Apple, for example, may use a different mechanism to access image pixels than Windows. This means that APIs may require a different set of information to perform operations. So, to work on shared code, we'd like to do the following:
Image.h : A header file to store class declaration.
Image.cpp : Code that will be shared across different end-points.
Image_windows.cpp : Code specific to Windows will reside here
Image_apple.cpp : Code specific to Apple will reside her
As seen in the preceding example, we are only mentioning the existence of a SImageInfo data structure while defining the blueprint of the CImage class.
SImageInfo's content is unknown. It is now the responsibility of the clients (Windows, Apple, and Android) to define that data structure and use it as needed. The design is already in place if we want to create an app for a new end-point 'X' in the future. We simply need to define SImageInfo for end-point 'X' and use it.
Please keep in mind that the example provided above is only one method. Design is all about debate and requirements. Many factors are considered when creating a good design. We can also have platform-specific classes such as CImageWindows and CImageApple, where we can put all platform-specific code.
An opaque pointer is a subset of an opaque data type, which is a data type declared to be a pointer to a record or data structure of some unspecified type.
Opaque pointers can be found in a variety of programming languages, including Ada, C, C++, D, and Modula-2.
If the language is strongly typed, programmes and procedures with no other knowledge of an opaque pointer type T can still declare variables, arrays, and record fields of that type, assign values of that type, and compare those values for equality. They cannot, however, de-reference such a pointer and can only change the object's content by calling a procedure that contains the missing information.