Introduction
A pointer is a variable whose value is the address of another variable, i.e., the memory location's direct address. Pointers help in saving memory space and help in accessing memory efficiently.
An opaque pointer is a pointer whose contents are abstracted at the time of definition. It is one of the powerful forms of abstraction for use in libraries. We will see how we can use the Opaque pointer.
Also see, Literals in C, Fibonacci Series in C++
Opaque Pointer
Opaque Pointers are made up of two words, opaque and pointers. Opaque means something which is not transparent, it means we cannot see through it, and the pointers are simply a variable that holds the address of another variable. Opaque Pointers is one of the powerful features in C and C++, which can be used just like abstraction as its contents are kept hidden at the time of its definition.
Now let's see how we can declare an opaque pointer.
Syntax
Struct STest* pStest;
So here, it is not possible to say which type of data STest structure is going to store by looking only at its definition.
It is safe for the opaque pointer to assign a NULL value.
pSTest= NULL;
Need for Opaque Pointer
Now, the question arises here: why do we need to use opaque pointers? So, there are places where we don't know the implementation, and we want the compiler to ignore it because it will be implemented by our clients while preparing the compilation unit.
Let's understand this by considering one situation:
Suppose we are creating an app that will deal with images; now we know that different people use different platforms, so we have to take care of all the platforms like windows, android, and apple. We have to share the code used by all the platforms, and then different end-points can have platform-specific code having a good design, which means that it should be scalable, robust, and flexible as per our requirements.
So we have a class named PImage, to deal with images, which exposes APIs to deal with image operations like scale, rotate, move, save, etc.
All the platforms we mentioned, like windows, android, and apple, have the same operations, so we need to define this class in a header file. But the important thing, the way an image is handled might differ across different platforms. Like windows can have a different mechanism to access pixels of an image than apple does. So to solve this problem and work on shared code, we would be doing something like this.
Image.h: A header file that will store class declaration.
// class PImage provides an API that deals with various operations and different platforms can implement these operations in // different ways.
class PImage
{
public:
PImage();
~PImage();
struct ShowImageInfo* pixelImageInfo;
void Move(int toX, int toY);
void Scale(double scaleFactorX,
double scaleFactorY);
void Rotate(double angle);
private:
void ImageInfo();
};
Image. cpp: This is the code that will be distributed among different end-points
PImage::PImage()
{
ImageInfo();
}
PImage::~PImage()
{
}
Image_windows.cpp : Code specific to Windows will reside here
struct ShowImageInfo
{
// Windows specific DataSet
};
void PImage::ImageInfo()
{
pixelImageInfo = new ShowImageInfo;
// Initialize windows specific info here
}
void PImage::Rotate()
{
// Make use of windows specific ShowImageInfo
}
Image_apple.cpp: Code specific to Apple will reside here
struct ShowImageInfo
{
// specific for apple
};
void PImage::ImageInfo()
{
pixelImageInfo = new ShowImageInfo;
// Initialize apple specific info here
}
void PImage::Rotate()
{
// Make use of apple specific ShowImageInfo
}
Now, we can clearly see from the above example that we have defined the PImage class's blueprint. We mention that there is a ShowImageInfo data structure, but the contents of ShowImageInfo are unknown. Now defining this data structure is the clients' responsibility, i.e., windows, apple, and android, and they can use it as per their requirements. Meanwhile, if we want to develop an app for a new end-point, 'X,' the design is already there. We only need to define ShowImageInfo for the end-point 'X' and use it accordingly.