Created
October 12, 2020 17:44
-
-
Save EmmanuelOga/469a52f59fb3c6322eb1e138d7dcc3ea to your computer and use it in GitHub Desktop.
When an override is not called because of move construction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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