- 336 名前:334 mailto:sage [2007/07/01(日) 18:02:45 ]
- >>335
なるほど継承ですか、実行時の多態性については意識し取りませんでした 確かにこの振る舞いをもたせたいのがAだけに限るわけではないですしね とりあえず参考にしてNVI使ってこうやってみました // 構造体A周辺の定義 struct Base { public: void print(std::ostream& os) const { _print(os); } private: virtual void _print(std::ostream& os) const =0; }; struct A : public Base { private: void _print (std::ostream& os) const { os << x; } public: int x; }; std::ostream& operator<<(std::ostream& os, const Base & base) { base.print(os); return os; } // 使用例 #include <iostream> int main() { A a; a.x = 1; std::cout << a; } …しかし鶏を捌くのに牛刀をなんとやらですね 実際に使うとなればここまでする必要も出てきそうな気もせんでも無いですが
|

|