Skip to content

Instantly share code, notes, and snippets.

@heathkit
Last active February 3, 2016 06:07
Show Gist options
  • Save heathkit/a97e2ddf5e131488f0d8 to your computer and use it in GitHub Desktop.
Save heathkit/a97e2ddf5e131488f0d8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
class Foobar
{
private:
int _array[10];
public:
Foobar() {
for( int i=0; i<10; i+=1)
_array[i] = i;
}
int Get(int index) {
return _array[index];
}
void Set(int index, int value) {
_array[index] = value;
}
};
class Apple
{
private:
std::shared_ptr<Foobar> _foobar;
public:
Apple(std::shared_ptr<Foobar> foobar)
: _foobar(foobar)
{
}
void Print() {
std::cout << "Apple::Print\n";
for( int i=0; i<10; i+=1)
std::cout << _foobar->Get(i) << " ";
std::cout << "\n";
}
};
class Pear
{
private:
std::shared_ptr<Foobar> _foobar;
public:
Pear(std::shared_ptr<Foobar> foobar)
: _foobar(foobar)
{
}
void Modify() {
_foobar->Set(0, 100);
}
};
int main() {
auto foobar = std::make_shared<Foobar>();
Apple apple(foobar);
Pear pear(foobar);
apple.Print();
pear.Modify();
apple.Print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment