Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of diamond problem in C++
#include <iostream>
using namespace std;
class Device {
void turn_on() {
cout << "Device is on." << endl;
class Computer: public Device {};
class Monitor: public Device {};
class Laptop: public Computer, public Monitor {
void turn_on() {
cout << "Laptop is on." << endl;
// uncommenting this function will resolve diamond problem
int main() {
Laptop Laptop_instance;
// Laptop_instance.turn_on();
// will produce compile time error
// if Laptop.turn_on function is commented out
// calling method of specific superclass
// treating Laptop instance as Monitor instance via static cast
static_cast<Monitor&>( Laptop_instance ).turn_on();
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment