使用友元类重载构造函数

发布于 2024-10-31 12:36:10 字数 1142 浏览 2 评论 0原文

我有一个类,它使用静态列表(示例中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┼── 2024-11-07 12:36:10

如果两个构造函数的代码完全相同,那么您可以将构造函数模板编写为:

template<class FriendType >
target(const FriendType &) 
{
    // Some nice initialization of the TargetData.
}

如果有一点差异,但大部分代码相同,那么您可以编写一个 init 模板函数,然后调用它来自两个构造函数:

  target(const firstFriend<int>& arg) 
  {
     init(arg);
     //other code
  }
  target(const secondFriend<int>& arg)
  {
     init(arg);
     //other code
  }
private:
  template<class FriendType >
  void init(const FriendType &) 
  {
     //common code
  }

If both constructors have exactly same code, then you can write a constructor template as:

template<class FriendType >
target(const FriendType &) 
{
    // Some nice initialization of the TargetData.
}

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:

  target(const firstFriend<int>& arg) 
  {
     init(arg);
     //other code
  }
  target(const secondFriend<int>& arg)
  {
     init(arg);
     //other code
  }
private:
  template<class FriendType >
  void init(const FriendType &) 
  {
     //common code
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文