Protect Smart Pointers by Providing Multiple Interfaces

Protect Smart Pointers by Providing Multiple Interfaces

In general, when a smart pointer is designed, the usual methods proivided are: T* operator->() const and T operator*() const. So, in functions such as foo(const SmartPointer& ptr) { }, what is meant by “const“? You can’t reassign ptr, of course?and further, you can only call const methods on the pointer itself. The method T* operator->() const can be called on ptr. Once dereferenced, you can call any method on the underlying protected object including non-const methods. Not what you expected?

Here is how you can declare your methods: assume that the smart pointer protects only one object. That object, however, has two interfaces which it implements: A “mutable” and an “immutable” interface. You then declare the methods as follows:

Mutable* operator->();


Immutable* operator->() const;

This works as follows:

foo(const SmartPointer& ptr)   {   ptr->bar_method();   }

The above code invokes this method:

Immutable* operator->() const

This, of course, returns an immutable interface. Obviously, an immutable interface will usually not have any methods that will change the state of the object.

On the other hand, the following code:

foo(SmartPointer& ptr)   {   ptr->bar_method2();   }

will invoke:

Mutable* operator->();

This gives you the full power of the mutable interface.

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