class Foo // Empty class
{
};
template <class T> // Abstract class
class Comparator
{
public:
virtual ~Comparator() {}
virtual bool operator()(const T& e1, const T& e2) = 0;
};
// Mother class that contains a map and some other methods that I did not copied here
template <class Key, class Value, class Comparator = std::less<Key> >
class Mother
{
private:
std::map<Key, Value, Comparator> data_;
};
// Daughter class that wants to use its own Comparator (called Nested)
class Daughter : public Mother<Foo, Foo, typename Daugher::Nested>
{
public:
class Nested : public Comparator<Foo>
{
bool operator()(const Foo& e1, const Foo& e2)
{
return true; // Not my real code. However, what would it mean to always return true in this kind of operation ? std::map would be pretty unusable, i guess ?
}
}
};
此代碼不能編譯爲G ++不能訪問Nested
,因爲我沒有訪問嵌套之前解決Daugher的模板。如果我寫Daughter<?????>::Nested
,我想可能會有效。但是,我需要給Daughter
自己的比較器,我無法訪問,因爲這需要我通過Daughter<?????>
和遞歸訪問它。解決模板與嵌套類
我非常確定,我正在嘗試做的事情在C++中是無效的,因爲在訪問它的嵌套類之前我應該解決Daughter
。然而,我的Nested
類非常簡單,並且不需要上層的類來真正定義。
所以,我可以宣佈Nested
外Daughter
,稱這是FooComparator
或東西,但它似乎吸塵器說,這是Nested
內Daughter
。
請注意,我不希望在Foo
範圍內申報operator<
,因爲在我的真實情況下,它代表的是城市,我認爲對城市申報operator<
並不是很乾淨。
是否有一個更清潔的選項,以便我可以聲明我的女兒類並要求它使用它自己的比較器?
和可能,',''裏面Daughter'。 – Jarod42