Before You Resort to Void *…

Before You Resort to Void *…

Void * can serve as a generic data pointer, yet it suffers from the well-known ailments associated with pointers: it can be NULL or a dangling pointer. Furthermore, it usually has to be cast back to its original type in order to access its data–a dangerous and sometimes costly operation if performed at run time. Luckily, C++ offers higher level mechanisms for genericity that are both safer and more efficient:

1. Templates. Templates are a better choice for generic (type-independent) algorithms and functions. In addition, they are safer since they do not involve pointer manipulations and can be checked at compile time:

 template  void  swap ( T f, T s); 

2. Inheritance. If you need a function or an algorithm that can be applied to a family of types, you may use a common base class from which all other related types are derived:

 class WindBase { public: virtual void Create()=0; virtual void Destroy() = 0; };class WindFrame: public WindBase { public: void Create(); void Destroy(); };class WindView: public WindBase { public: void Create(); void Destroy();}; };void DestroyWindow(WindBase &anyWindow); //for all objects derived from WindBase

Please note that void* should be used in very low-level operations–for example, manipulating hardware buffers or in system routines such as memset() and malloc().

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes