In embedded firmware, drivers are often built by layering multiple capability classes.
When these layers inherit from a shared hardware base, a diamond inheritance structure can occur.
If this structure is implemented incorrectly, it can lead to:
Your task is to observe this problem and fix it correctly using Embedded C++ best practices.
Scenario
You are given the following inheritance structure:
DeviceCore
/ \
CommLayer PowerLayer
\ /
SensorDriver
DeviceCore represents shared hardware stateCommLayer and PowerLayer both inherit from DeviceCoreSensorDriver inherits from both layersWhen inheritance is non-virtual, SensorDriver contains two separate copies of DeviceCore.
Objective
Modify the program so that:
DeviceCore exists inside SensorDriverDeviceCore is initialized exactly once
Rules (Strict)
You must follow all rules below:
Input
One signed integer value:
id Program Flow (Mandatory Order)
idSensorDriver object using idExpected Output (After Fix)
Device core initialized
Device ID <id>
⚠️ Note
Before fixing the design,Device core initialized would be printed twice.
Example
Input:
42
Output:
Device core initialized
Device ID 42
Input
0
Expected Output
Device core initialized Device ID 0