#if 0
#include <iostream>
#include <memory>
void fourthFunction(std::unique_ptr<int32_t> responseCode) {
*responseCode = 500;
// unique_ptr goes out of scope here, releasing the resource
}
void thirdFunction(std::unique_ptr<int32_t> responseCode) {
*responseCode = 404;
fourthFunction(std::move(responseCode)); // Transfer ownership to fourthFunction
}
void secondFunction(std::unique_ptr<int32_t> responseCode) {
*responseCode = 200;
thirdFunction(std::move(responseCode)); // Transfer ownership to thirdFunction
}
void firstFunction(std::unique_ptr<int32_t> responseCode) {
*responseCode = 100;
secondFunction(std::move(responseCode)); // Transfer ownership to secondFunction
}
int main() {
std::unique_ptr<int32_t> responseCode = std::make_unique<int32_t>(0); // Initialize as 0
firstFunction(std::move(responseCode)); // Start by transferring ownership
// At this point, responseCode has been moved and no longer owns the resource
// std::cout << *responseCode; // Error: responseCode is now null after ownership was moved
return 0;
}
#endif
#include <iostream>
#include <memory>
void fourthFunction(std::shared_ptr<int32_t> responseCode) {
*responseCode = 500;
}
void thirdFunction(std::shared_ptr<int32_t> responseCode) {
*responseCode = 404;
fourthFunction(responseCode); // Pass shared ownership to fourthFunction
}
void secondFunction(std::shared_ptr<int32_t> responseCode) {
*responseCode = 200;
thirdFunction(responseCode); // Pass shared ownership to thirdFunction
}
void firstFunction(std::shared_ptr<int32_t> responseCode) {
*responseCode = 100;
secondFunction(responseCode); // Pass shared ownership to secondFunction
}
int main() {
std::shared_ptr<int32_t> responseCode = std::make_shared<int32_t>(0); // Initialize as 0
firstFunction(responseCode); // Start from the first function
std::cout << "Final Response Code: " << *responseCode << std::endl; // Output: 500
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: