Skip to content

Instantly share code, notes, and snippets.

@mmomtchev
Created August 16, 2023 15:00
Show Gist options
  • Save mmomtchev/0544f11b1ff104c7b5498e6aed8cbf9b to your computer and use it in GitHub Desktop.
Save mmomtchev/0544f11b1ff104c7b5498e6aed8cbf9b to your computer and use it in GitHub Desktop.
Copying `std::exception` with an error message
#include <iostream>
int main() {
auto err = std::logic_error("some error");
std::cout << err.what() << std::endl;
// using the derived copy constructor
std::logic_error err2 = err;
std::cout << err2.what() << std::endl;
// what() is virtual
std::exception &err3 = err;
std::cout << err3.what() << std::endl;
// This slices the object
std::exception err4 = err;
std::cout << err4.what() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment