Opaque Pointer in C++
Overview
The word opaque means something through which we can not see through. An opaque pointer in C++ is a pointer that points to a data structure whose content is not visible at the time of its declaration.
What is an Opaque Pointer in C++?
A pointer that points to a data structure whose data is not visible at the time of the pointer's declaration is called an opaque pointer.
For Example:
In the above example, api is a structure while the pointer ps is an opaque pointer because we can not recognize the contents of the structure just by looking at the pointer declaration.
C++ allows us to assign NULL to opaque pointers.
For Example:
Why Use an Opaque Pointer?
With the help of an opaque pointer in C++, we can hide the implementation details of an interface from the clients. Because the details are hidden, we can change the implementation of our program without affecting the program at the client's end. This is beneficial for not only the client but the programmer as well. Programmers can create simple interfaces while hiding most of the program details in different files, making it easy for them to write and manage the codebase.
The primary purpose of opaque pointers is to provide the clients with a way to hold the reference to an object (pointer) without revealing anything about the object’s implementation. The client can use this pointer for different purposes, such as storing it in its data structures, comparing two such pointers to check if they are the same, etc. However, the client can not dereference the pointers to look at what is present in the object(s).
Example
Consider an application that is used to edit images. This application needs to work on different operating systems like Windows and Mac. To develop this application, we can have some shared code that is used by all platforms (operating systems) and some platform-specific code.
To edit the images, we can create a class that exposes an API in order to perform different operations like rotating, resizing, etc. Because all the platforms will have the same image operations, we can define this class in a header file.
Now, how an operation is performed depends on the operating system on which it is being performed. For example, Windows may have a different method to access the data of an image compared to Mac. So, we will create platform-specific code to perform the operations. We will also provide different endpoints for the API so that the users can access the application based on the operating system they are using.
The above-discussed application can be implemented like this:
Image.h File
We have created a header file containing a class CImage that provides an API to handle the image operations. These operations will be implemented in different ways based on the platform they will be performed in.
Image.cpp File
This file is used to define the constructor and destructor of the CImage class. The constructor calls the InitImageInfo() method. The code inside this method will be written in accordance with the operating system it will work on.
Image_windows.cpp File
In this file, we will write the code that is specific to the Windows operating system.
Image_apple.cpp File
In this file, we will write the code that is specific to the Mac operating system.
From the above example, we can see that while defining the CImage class, the Windows and the Mac-specific code is not being shown publicly to the clients (Windows, Mac). The SImageInfo structure is public. However, the contents of this structure are unknown. It is the responsibility of the client to define this data structure and use it as per their requirement.
Conclusion
- Opaque pointers in C++ point to those data structures whose data is not visible at the time of pointer declaration.
- Opaque pointers in C++ can store NULL value.
- Opaque pointers in C++ are (generally) used to hide the implementation details from the clients.