使用友元类重载构造函数
我有一个类,它使用静态列表(示例中的firstFriend)或动态列表(示例中的secondFriend)进行初始化。我不想为示例编写列表功能,因为它并不重要。关键问题是第一个朋友和第二个朋友是朋友。 “目标”类的构造函数的代码是相同的:如果我重载构造函数,我将复制完全相同的代码。我无法模板化构造函数,因为它不起作用。
这是示例(注意:firstFriend 和 SecondFriend 可能看起来相同,但它们在实际代码中并不相同,这只是一个大幅简化的模型,它们的属性和功能之间的差异不会在“目标”中产生任何差异“类构造函数,因为两个类使用的公共接口部分完全相同):
template <class T>
class firstFriend
{
public:
firstFriend() {};
firstFriend(const T& t) {};
private:
T tAttribute;
};
template <class T>
class secondFriend
{
public:
secondFriend() {};
secondFriend(T t) : tAttribute(t) {};
friend class firstFriend<T>;
private:
T tAttribute;
};
class target
{
public:
target(const firstFriend<int>&)
{
// Some nice initialization of the TargetData.
}
target(const secondFriend<int>&)
{
// Exactly the same initialization as above.
// To the single character.
};
private:
firstFriend<int> TargetData;
};
问题:如何重载构造函数而不编写(复制/粘贴)相同的代码两次?我尝试过对构造函数进行模板化,但没有成功。也许是隐式强制转换?什么是最有效的方法(第一个朋友和第二个朋友是巨大的数据列表)。提前致谢!
I have a class that uses either a static list (firstFriend in the example) or a dynamic list (secondFriend in the example) for initialization. The list functionality I didn't want to write for the example because it is not important. The key problem is that the firstFriend and the secondFriend are, well, friends. The code of the constructors of the class "target" is the same: if I overload the constructors I'm duplicating exactly the same code. I cannot template the constructors because it doesn't work.
Here's the example (NOTE: the firstFriend and secondFriend may look the same, but they are NOT the same in the ACTUAL code, this is just a heavily reduced model, the differences between their attributes and functionality does not make any differrence in the "target" class constructor because the parts of the public interface is used from both classes that are exactly the same):
template <class T>
class firstFriend
{
public:
firstFriend() {};
firstFriend(const T& t) {};
private:
T tAttribute;
};
template <class T>
class secondFriend
{
public:
secondFriend() {};
secondFriend(T t) : tAttribute(t) {};
friend class firstFriend<T>;
private:
T tAttribute;
};
class target
{
public:
target(const firstFriend<int>&)
{
// Some nice initialization of the TargetData.
}
target(const secondFriend<int>&)
{
// Exactly the same initialization as above.
// To the single character.
};
private:
firstFriend<int> TargetData;
};
Question: how do I overload constructors without writing (copy/paste-ing) the same code twice? I have tried templating the constructors, but it didn't work. Implicit cast maybe? What would be the most efficient way (the firstFriend and secondFriend are HUGE lists of data). Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果两个构造函数的代码完全相同,那么您可以将构造函数模板编写为:
如果有一点差异,但大部分代码相同,那么您可以编写一个
init
模板函数,然后调用它来自两个构造函数:If both constructors have exactly same code, then you can write a constructor template as:
If there is a little difference, but most of the code is same, then you can write an
init
template function, and call it from both constructors as: