我有一个模板类,其中多个类正在继承,从根本上制造了一些用户友好的构建器,从而隐藏了在干燥时在不同构建器上不需要的功能。
但是,我在构建器类型之间切换时遇到了麻烦。我宁愿使用 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 =
,它只是没有从模板中生成一个
I have a template class that multiple classes are inheriting from to basically make some user-friendly builders, hiding the functions that are not required on different builders, while following DRY.
However, I'm having trouble switching between the builder types. I'd prefer to use the operator=
to switch without hassle and have the following code.
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;
}
Classes
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;
};
Code that says no
// 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;
Except I might be doing something I shouldn't be doing with template classes, although others seem to have success with templates in the operators, so assume possible, and I'm making some kind of mistake.
More info: The error I get is:
Error C2679 binary '=': no operator found which takes a right-hand operand
of type 'BuilderRootList' (or there is no acceptable conversion)
So It's definitely looking for my operator=
, it's just not generating one from the template
发布评论
评论(2)
当您执行模板继承时,必须在基本类成员的情况下明确说明。 More read:
在您的情况下,成员
text
和operator =
,可以是使用声明通过带来。
live demo
请注意,这将使成员
使用builderFunctions&lt;文本
公开,如果不是想要的,请在其他答案中考虑@jarod42 的建议。When you do the template inheritance, you have to be explicit in case of base classes members. More read:
In your case, the members
text
, andoperator=
, can be brought viausing
declaration.Live Demo
Note that, this will make the member
using BuilderFunctions<BuilderGenericList>::text
public, if this is not what wanted, consider the suggestion by @Jarod42 in other answer.builderFunctions&lt; t1&gt;
无法访问private
/受保护
builderFunctions&lt; t2&gt;
(带有t1
t1
) /code>!=t2
):添加访问者,或使它们friend
。此外,您还必须使用
operator =
:demo
BuilderFunctions<T1>
cannot accessprivate
/protected
member ofBuilderFunctions<T2>
(withT1
!=T2
): add accessor, or make themfriend
.in addition, you also have to put using for
operator=
:Demo