When is virtual inheritance needed?

When is virtual inheritance needed?

Multiple inheritance is a powerful and useful feature in C++, but it can lead to a problem known as the DDD or “Deadly Diamond of Derivation”, as in this case:

class ElectricAppliance{ int voltage, int Hertz ; public: //…constructor and other useful methods int getVoltage () const { return voltage; } int getHertz() const {return Hertz; } };

class Radio : public ElectricAppliance {…};class Tape : public ElectricAppliance {…};

class RadioTape: public Radio, public Tape { //multiple inheritance //… };void main() {

RadioTape rt;

int voltage = rt.getVoltage(); //compilation Error — ambiguous //call; //two copies getVoltage() exist in rt: //one from Radio and one from Tape. //Also: which voltage value should be //returned? }//end main()

The problem is clear: rt is derived simultaneously from two base classes, each having its own copy of the methods and data members of ElecctricAppliance. As a result, rt has two copies of ElectricAppliance. This is the DDD. However, giving up multiple inheritance will lead to a design compromise. So in cases where reduplication of data and methods from a common base class is undesirable, virtual inheritance should be used:

class Radio : virtual public ElectricAppliance {…}; class Tape : virtual public ElectricAppliance {…};class RadioTape: public Radio, public Tape { //multiple inheritance };

As a result, class RadioTape contains a single instance of ElectricAppliance shared by Radio and Tape, so there are no ambiguities, no memory waste, and no need to give up the powerful tool of multiple inheritance:

void main() {RadioTape rt;int voltage = rt.getVoltage(); //now OK}//end main()

Share the Post:
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

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as