簡體   English   中英

重載 operator= 在模板類型之間切換

[英]Overloaded operator= to switch between template types

我有一個模板類,多個類從中繼承,基本上可以制作一些用戶友好的構建器,隱藏不同構建器不需要的功能,同時遵循 DRY。

但是,我在構建器類型之間切換時遇到了麻煩。 我更喜歡使用operator=來輕松切換並擁有以下代碼。

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= ,它只是沒有從模板中生成一個

當您進行模板繼承時,您必須明確基類成員的情況。 更多閱讀:

在您的情況下,成員textoperator=可以通過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在其他答案中的建議。

BuilderFunctions<T1>無法訪問BuilderFunctions<T2>private / protected成員(使用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;
};

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM