48. Resolve Driver Collision

#include <iostream>
using namespace std;

// Motor driver A
namespace DriverA {
    void init() {
        cout << "A INIT";
    }
}

// Motor driver B
namespace DriverB {
    void init() {
        cout << "B INIT";
    }
}

int main() {
    int x;
    cin >> x;

    if (x == 1) {
        DriverA::init();   // call driver A
    }
    else if (x == 2) {
        DriverB::init();   // call driver B
    }

    return 0;
}

Explanation & Logic Summary:

  • Both drivers expose a function named init()
  • Namespaces prevent symbol collisions between modules
  • DriverA::init() and DriverB::init() explicitly select the correct driver
  • This demonstrates the core purpose of namespaces in Embedded C++

Firmware Relevance & Real-World Context:

Embedded projects often include:

  • vendor HALs
  • multiple driver implementations
  • protocol stacks
  • RTOS layers
  • duplicated function names (init, start, read, etc.)

Namespaces prevent conflicts when integrating multiple codebases.
This is especially important in large embedded systems or when mixing vendor SDKs with custom drivers.