【初心者歓迎】C/C++室 Ver.8【環境依存OK】
■ このスレッドは過去ログ倉庫に格納されています
0575デフォルトの名無しさん
NGNGDerivedBは論理的にDerivedAでもあるのだから、
DerivedBのインスタンスが生成されたときにDerivedAのカウントが増えるのは自然。
>>572が言っているのは継承を一度しか許さぬ云々ではなくそのことだと思う。
template <typename Derived>
struct countable {
protected:
countable(bool b) : top(b) {if(b) ++count;}
int get_count() {return count;}
~countable() {if(top) --count;}
private:
bool top;
static int count;
};
template <typename Derived>
int countable<Derived>::count;
class CBase {
virtual int get_count() = 0;
};
class CDerivedA: public CBase, private countable<CDerivedA> {
typedef countable<CDerivedA> countable_type;
public:
CDerivedA(bool top = true) : countable_type(top) {}
virtual int get_count() { return countable_type::get_count();}
};
class CDerivedB: public CDerivedA, private countable<CDerivedB> {
typedef countable<CDerivedB> countable_type;
public:
CDerivedB(bool top = true) : CDerivedA(false) , countable_type(top) {}
virtual int get_count() { return countable_type::get_count(); }
};
■ このスレッドは過去ログ倉庫に格納されています