example4_third_party_APIs.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>

// Simulating a third-party API that isn't const-correct
void legacy_strcpy(char* dest, char* src) {
    std::strcpy(dest, src);
}

int main() {
    const char* source = "Hello, World!";
    char destination[20];

    // Using const_cast to call the legacy function
    legacy_strcpy(destination, const_cast<char*>(source));

    std::cout << "Copied string: " << destination << std::endl;
    return 0;
}
Back to constant_cast