Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Created March 26, 2017 20:26
Show Gist options
  • Save bl4ckb0ne/7eb407f0172b7493a9c19f7ce622c435 to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/7eb407f0172b7493a9c19f7ce622c435 to your computer and use it in GitHub Desktop.
Access an operator of an object inside a smart ptr http://coliru.stacked-crooked.com/a/f811a7e99cb58873
#include <iostream>
#include <vector>
#include <string>
#include <memory>
struct Foo
{
Foo(int size) : words(size, "Foo")
{}
std::vector<std::string> words;
std::string operator [](size_t i)
{
return words[i];
}
};
int main()
{
Foo f(12);
std::cout << f[0] << '\n';
std::unique_ptr<Foo> ptr(new Foo(12));
std::cout << (*ptr)[0] << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment