Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Created October 12, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmmanuelOga/469a52f59fb3c6322eb1e138d7dcc3ea to your computer and use it in GitHub Desktop.
Save EmmanuelOga/469a52f59fb3c6322eb1e138d7dcc3ea to your computer and use it in GitHub Desktop.
When an override is not called because of move construction.
#include <iostream>
#include <stdexcept>
#include <utility>
struct Base {
virtual const char *test() const { return "Base!"; }
Base() = default;
Base(const Base& other) noexcept {
std::cout << "Copy CTOR" << std::endl;
}
Base(Base&& other) noexcept {
std::cout << "Move CTOR" << std::endl;
}
Base operator=(Base&& other) noexcept {
std::cout << "Move assigment." << std::endl;
return *this;
}
Base& operator=(const Base& other) noexcept {
std::cout << "Copy assigment." << std::endl;
return *this;
}
};
struct Deriv : Base {
const char *test() const override { return "Deriv!"; }
};
int main() {
Deriv d1 = Deriv{}; // d1.test() calls Deriv's override.
Base& b = d1; // b.test() calls Deriv's override.
Base b1 = Deriv{}; // b.test() calls Base implementation... is a MOVE constructor!
std::cout << d1.test() << std::endl;
std::cout << b.test() << std::endl;
std::cout << b1.test() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment