1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
class City {
public:
struct House_shared {
House_shared(const City& myplace_arg, int color_arg);
struct House {
House(const House_shared& hs_arg);
int getColor() const { return hs.color; }
private:
const House_shared& hs;
};
House house_a, house_b;
private:
const City& myplace;
int color;
};
struct Park_shared {
Park_shared(const City& myplace_arg, int color_arg);
struct Park {
Park(const Park_shared& ps_arg);
int getColor() const { return ps.color; }
private:
const Park_shared& ps;
};
Park park_a, park_b;
private:
const City& myplace;
int color;
};
City(int house_color = 1, int park_color = 2);
House_shared hs;
Park_shared ps;
};
City::City(int house_color, int park_color)
: hs { *this, house_color },
ps { *this, park_color }
{}
City::House_shared::House_shared(const City& myplace_arg, int color_arg)
: house_a { *this }, house_b { *this },
myplace { myplace_arg }, color { color_arg }
{}
City::House_shared::House::House(const House_shared& hs_arg) : hs { hs_arg } {}
City::Park_shared::Park_shared(const City& myplace_arg, int color_arg)
: park_a { *this }, park_b { *this },
myplace { myplace_arg }, color { color_arg }
{}
City::Park_shared::Park::Park(const Park_shared& ps_arg) : ps { ps_arg } {}
int main()
{
City city_a(13, 14), city_b(666, 667);
std::cout << "\ncity_a.hs.house_a::color: " << city_a.hs.house_a.getColor()
<< "\ncity_a.hs.house_b::color: " << city_a.hs.house_b.getColor()
<< "\ncity_a.ps.park_a::color: " << city_a.ps.park_a.getColor()
<< "\ncity_a.ps.park_b::color: " << city_a.ps.park_b.getColor()
<< "\ncity_b.hs.house_a::color: " << city_b.hs.house_a.getColor()
<< "\ncity_b.hs.house_b::color: " << city_b.hs.house_b.getColor()
<< "\ncity_b.ps.park_a::color: " << city_b.ps.park_a.getColor()
<< "\ncity_b.ps.park_b::color: " << city_b.ps.park_b.getColor()
<< '\n';
return 0;
}
|