Skip to content

Instantly share code, notes, and snippets.

@jonathanvdc
Last active March 21, 2017 17:50
Show Gist options
  • Save jonathanvdc/faa38c7a8320c125d04d9d4adb756ce2 to your computer and use it in GitHub Desktop.
Save jonathanvdc/faa38c7a8320c125d04d9d4adb756ce2 to your computer and use it in GitHub Desktop.
#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