Last active
March 21, 2017 17:50
-
-
Save jonathanvdc/faa38c7a8320c125d04d9d4adb756ce2 to your computer and use it in GitHub Desktop.
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 <vector> | |
class Alias | |
{ | |
private: | |
Alias(const std::vector<int>& data) : data(data) { } | |
// Delete this to make sure that we don't accidentally make a copy. | |
Alias(const Alias&) = delete; | |
public: | |
// It's super important for this move constructor to be both public and implemented | |
// (in this case, by spelling `= default`. | |
Alias(Alias&&) = default; | |
std::vector<int> data; | |
static Alias CreateAlias(const std::vector<int>& data) | |
{ | |
// Will call the Alias(const std::vector<int>&) ctor first, then the Alias(Alias&&) ctor. | |
return {data}; | |
} | |
}; | |
int main() | |
{ | |
Alias alias_instance = Alias::CreateAlias({1, 2, 3}); | |
std::cout << alias_instance.data[0] << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment