#2
by かずま » 6年前
staticメンバなら OK だと思います。
ただし、定義は外部で行わないとだめなようです。
コード:
#include <iostream>
using std::cout;
struct ColorCode {
int r, g, b;
ColorCode(int r = 0, int g = 0, int b = 0) : r(r), g(g), b(b) { }
ColorCode(const ColorCode& cc) : r(cc.r), g(cc.g), b(cc.b) { }
static const ColorCode Red;
};
struct Rect {
int left, top, right, bottom;
Rect(int left = 0, int top = 0, int right = 160, int bottom = 120)
: left(left), top(top), right(right), bottom(bottom) { }
void Draw(const ColorCode& cc) {
cout << "(" << cc.r <<", " << cc.g << ", " << cc.b << ")\n";
}
};
ColorCode const ColorCode::Red = { 255, 0, 0 };
void f() {
Rect a(0,0, 320, 240);
a.Draw(ColorCode(255, 0, 0));
a.Draw({255, 0, 0});
a.Draw(ColorCode::Red);
}
int main() { f(); }
staticメンバなら OK だと思います。
ただし、定義は外部で行わないとだめなようです。
[code]
#include <iostream>
using std::cout;
struct ColorCode {
int r, g, b;
ColorCode(int r = 0, int g = 0, int b = 0) : r(r), g(g), b(b) { }
ColorCode(const ColorCode& cc) : r(cc.r), g(cc.g), b(cc.b) { }
static const ColorCode Red;
};
struct Rect {
int left, top, right, bottom;
Rect(int left = 0, int top = 0, int right = 160, int bottom = 120)
: left(left), top(top), right(right), bottom(bottom) { }
void Draw(const ColorCode& cc) {
cout << "(" << cc.r <<", " << cc.g << ", " << cc.b << ")\n";
}
};
ColorCode const ColorCode::Red = { 255, 0, 0 };
void f() {
Rect a(0,0, 320, 240);
a.Draw(ColorCode(255, 0, 0));
a.Draw({255, 0, 0});
a.Draw(ColorCode::Red);
}
int main() { f(); }
[/code]