我有一個模板類,多個類從中繼承,基本上可以制作一些用戶友好的構建器,隱藏不同構建器不需要的功能,同時遵循 DRY。
但是,我在構建器型別之間切換時遇到了麻煩。我更喜歡使用operator=to 輕松切換并擁有以下代碼。
template<class T> class BuilderFunctions
{
protected:
std::string text;
public:
template<class Other>
BuilderFunctions<T>& operator=(const Other& other);
};
template <class T>
template <class Other>
BuilderFunctions<T>& BuilderFunctions<T>::operator=(const Other& other)
{
// yes, I need same object protection
text = other.text;
return *this;
}
課程
class BuilderGenericList : public BuilderFunctions<BuilderGenericList>
{
public:
BuilderGenericList() = default;
};
// Build configuration details
class BuilderRootList : public BuilderFunctions<BuilderRootList>
{
private:
// I'll delete the functions I don't want accessed here
public:
BuilderRootList() = default;
};
說不的代碼
// Real world you'd build the root first and switch out to another builder or be using a prototype to seed another builder
BuilderRootList cmakeRoot;
BuilderGenericList list;
// Separate to make sure it's not trying to use cop constructor
list = cmakeRoot;
除了我可能正在做一些我不應該對模板類做的事情,盡管其他人似乎在運算子中的模板方面取得了成功,所以假設可能,我犯了某種錯誤。
更多資訊:我得到的錯誤是:
Error C2679 binary '=': no operator found which takes a right-hand operand
of type 'BuilderRootList' (or there is no acceptable conversion)
所以它肯定在尋找我的operator=,它只是沒有從模板中生成一個
uj5u.com熱心網友回復:
當您進行模板繼承時,您必須明確基類成員的情況。更多閱讀:
- 為什么我必須通過 this 指標訪問模板基類成員?
- 對基類成員資料的派生模板類訪問
- 訪問從模板類派生的類中的基成員函式
在您的情況下,成員text, 和operator=, 可以通過using宣告帶來。
class BuilderGenericList : public BuilderFunctions<BuilderGenericList>
{
public:
BuilderGenericList() = default;
using BuilderFunctions<BuilderGenericList>::text;
using BuilderFunctions<BuilderGenericList>::operator=;
// ... so on, other members from base if needed!
};
// Build configuration details
class BuilderRootList : public BuilderFunctions<BuilderRootList>
{
public:
BuilderRootList() = default;
using BuilderFunctions<BuilderRootList>::text;
using BuilderFunctions<BuilderRootList>::operator=;
// ... so on, other members from base if needed!
};
現場演示
請注意,這將使成員using BuilderFunctions<BuilderGenericList>::text公開,如果這不是想要的,請考慮@Jarod42在其他答案中的建議。
uj5u.com熱心網友回復:
BuilderFunctions<T1>無法訪問private/protected成員BuilderFunctions<T2>(使用T1!= T2):添加訪問器,或制作它們friend。
此外,您還必須使用 for operator=:
template<class T>
class BuilderFunctions
{
template <typename U> friend class BuilderFunctions;
protected:
std::string text;
public:
template<class Other>
BuilderFunctions<T>& operator=(const Other& other);
};
template <class T>
template <class Other>
BuilderFunctions<T>& BuilderFunctions<T>::operator=(const Other& other)
{
//yes, I need same object protection
text= other.text;
return *this;
}
class BuilderGenericList : public BuilderFunctions<int>{
public:
using BuilderFunctions<int>::operator=;
BuilderGenericList() = default;
};
//Build configuration details
class BuilderRootList : public BuilderFunctions<float>
{
private:
//I'll delete the functions I don't want accessed here
public:
using BuilderFunctions<float>::operator=;
BuilderRootList() = default;
};
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495877.html
