Created
March 26, 2017 20:26
-
-
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
This file contains hidden or 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> | |
#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