Opaque Pointer28 Aug 2024 | 3 min read 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. ConclusionAn 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. |
Level Order Traversal in Spiral form
What is a binary tree? A binary tree is a data structure that consists of nodes organized hierarchically. Each node has at most two children, typically the left and right child. The root node is the topmost node in the tree, and the leaf nodes are the...
16 min read
C++ Program to calculate the Bitonicity of an Array
In this article, we will discuss a C++ program to calculate the Bitonicity of an Array. The Bitonicity of an array is - Initialized to 0 Incremented to one if the subsequent element is more than the ious value. Decremented to 1 if the following element is lesser than the...
2 min read
Default arguments in C++
In a function, arguments are defined as the values passed when a function is called. Values passed are the source, and the receiving function is the destination. Now let us understand the concept of default arguments in detail. Definition A default argument is a value in the function declaration...
4 min read
Nesbitt's Inequality in C++
Nesbitt's i? a mathematicall? expressed inequality relation th?t connects the arithmetic mean and the harmonic mean of three positive numbers ?, b ?nd c. More precisely, it states that the sum of the reciprocals οf the arithmetic means of the pairs of these numbers i? greater...
13 min read
Static Keyword in C++ and JAVA
In C++, the keyword static is used to give element unique properties. Static elements are only allocated storage in the static storage area once during the program's lifetime. And they are valid for the duration of the programme. The following are examples of static keywords: Functions with...
3 min read
std::string::crbegin() and std::string::crend() in C++
In C++, std::string::crbegin() and std::string::crend() are functions that are members of the std::string class, which was added in C++11. They provide access to a string's reversed iterators, allowing users to iterate by traversing the string's elements in the opposite direction. In this article, we will discuss...
2 min read
Difference between std::swap and std::vector::swap
in C++. But before discussing the difference, we must know about the std::swap and std::vector::swap in C++. What is std::swap? The utility function std::swap is defined in the C++ standard library's <algorithm> header. It allows swapping the values of two objects. Syntax: The syntax of std::swap is: template<class T> void swap(T& a,...
4 min read
How to Create and Use ''unique_ptr'' Instances
What are Instances in C and C++ programming languages? In C programming, an instance is a single occurrence of an object or data structure. For example, if you have a class called "Dog," each dog you create from that class would be an instance of the "Dog"...
9 min read
C++ Program for Iterative Quick Sort
In this article, we will discuss the C++ program for iterative quick sort. But before going to its implementation, we must know about the iterative quick sort with its algorithm and example. One popular sorting algorithm well-known for its practical efficiency and efficacy is called 'Quick Sort'....
4 min read
Long Data Type C++
C++ is a flexible and strong programming language combining the procedural and object-oriented programming paradigms. C++, created as an extension of the C programming language, adds important features like classes and objects, making it possible to write modular and reusable code. One of C++'s advantages is...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India