30. Select Driver Overload

#include <iostream>
using namespace std;

void init(int pin) {
    cout << "PIN " << pin;
}

void init(int* portPtr) {
    cout << "PORT NULL";
}

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

    if (mode == 1) {
        int pin;
        cin >> pin;
        init(pin);        // calls int overload
    }
    else if (mode == 2) {
        init(nullptr);    // calls pointer overload
    }

    return 0;
}

Explanation & Logic Summary:

  • Two overloads exist: one takes int, one takes int*
  • Using 0 or NULL can select the wrong overload
  • nullptr enforces pointer overload selection
  • Output confirms which overload was executed

Firmware Relevance & Real-World Context:

  • HAL APIs often overload based on:
    • pin vs port
    • index vs pointer
    • buffer vs size
  • Passing 0 can silently:
    • select pin 0 unintentionally
    • bypass pointer-based initialization
  • nullptr eliminates these ambiguity-driven bugs

 

 

 

Loading...

Input

1 7

Expected Output

PIN 7