Inheritance design

Inheritance design

Suppose I’ve got the following class hierarchies:

Surface_PlotContourSurface_Plot, child of Surface_PlotSurface_DataGridSurface_Data, child of Surface_DataSurface_Plot contains a Surface_Data object, ContourSurface_Plot contains a GridSurface_Data object
.What is the most elegant way to arrange the data members of the Surface_Plot hierarchy so that the Surface_Plot class can access the GridSurface_Data object’s Surface_Data behaviors, and ContourSurface_Plot can access its extended behaviors? My approach has been to make the xxxData objects pointers, and to have both a GridSurface_Data and Surface_Data pointer referencing the same data member.

This route to a solution seems terribly ugly and error-prone. Furthermore, my real hierarchies are much longer, resulting in a plethora of redundant pointers of increasingly sophisticated types.

There must be a cleaner way!

You are right ? having two pointers is a problem and could end up becominga maintenance nightmare. The trick is to use the right type in the derived class to access the extended behavior. Consider:

class Surface_Plot{protected:   Surface_Data *data_;};class ContourSurface_Plot : public Surface_Plot{public:   void useExtendedOperation ()   {     GridSurface_Data * g = dynamic_cast(data);     if(g) g->doSomething ();   }};
That’s the basic idea. If the GridSurface_Data needs to be usedin a lot of places, it might be worthwhile to incorporate it asa data member and initialize it in the constructor of ContourSurface_Plot.This is similar to your multiple pointer solution. In this case you want to keep a reference in the derived class to prevent accidental deletion.

In an implementation I would move the burden of maintaining this extra pointer to another class that will do the cast, etc., as in:

template class Surface_Data_Container : public Surface_Plot{protected:   Surface_Data_Container() : data_(dynamic_cast(Surface_Plot::data_))   {      assert(data_ != 0);   }   T *data_;};
and then inherit ContourSurface_Plot from Surface_Data_Container:
class ContourSurface_Plot : public Surface_Data_Container{public:   void useExtendedOperation ()   {     data_->doSomething ();     // accesses Surface_Data_Container::data_ which is the    // right type..   }};

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