复制构造函数调用 C++

Copy constructor call C++

所以我对 C++ 中的复制构造函数感到很困惑。我有以下代码:

class creature   /* abstract class*/
{
    private:
        string name;
        int longevity;
        creature_society * cs;
    public:
        creature(int,int,int,creature_society*);
        //creature(const creature&);
        virtual ~creature();

        virtual int is_a_good() =0;
};

class good_creature : public creature
{
     public:
         good_creature(int,int,creature_society*);
         //good_creature(const good_creature&);
         ~good_creature();

         int is_a_good() //returns 1
};

class bad_creature : public creature
{
    public:
         bad_creature(int,int,creature_society*);
         //bad_creature(const bad_creature&);
         ~bad_creature();

          int is_a_good(void); //returns 0
}

所以我有一个名为 creature 的摘要 class,一个 good_creature 和一个 bad_creature ,它们是 children class creature .

在我的程序中,我还有一个名为 society 的数组,其类型为 creature* objects。如果我的生物通过某个条件被定义为良好,我会为其分配 space 并将其作为 good_creature 存储在 society 数组中。坏生物也是如此。我按照以下代码中的描述构建它:

society = new creature*[M];
for(i=0;i<M;i++)
{
      if(condition)
            society[i] = new good_creature(L,good,this);
      else
            society[i] = new bad_creature(L,bad,this);
}

所以我必须创建一个纯虚函数:creature::clone(int position) 如果它是 good_creaturebad_creature,它必须删除 society[pos] 并使通过 复制构造函数 .

society[pos-1] 副本

例如我的 good_creature::clone(int position) 是这样的:

  void good_creature::clone(int position)
  {
      int cop_pos=position -1;     //getting the position before that in order to copy it
      delete society[pos];
      society[pos] = new good_creature( *society[cop_pos] ); 
      //....
   }

我得到一个错误,因为 society[cop_pos]creature* 类型。我尝试将它投射到 good creature 但不幸的是我不断出错。是因为我没有正确调用复制构造函数,还是因为我没有正确转换?有任何想法吗?这让我困惑了 2 天。请记住,我是新手,可能做错了什么。

此外,我不需要定义自己的复制构造函数,因为 society[i] 中的所有元素都指向由 creature_society * cs 定义的同一个 object,所以我尝试使用默认构造函数,因为我不需要 deep copy.

感谢您的宝贵时间。

更新

一个class我忘了提我建构社会的方式

class creature_society
{
    private:
        int N; // number of the creatures we want to be made in society
        creature ** society;
    public:
        creature_society(int,int);
        ~creature_society();
 };

问题是 societycreature 的数组,而不是 good creature 的数组,因此复制构造函数不适用。

您可以为 good_creaturebad_creature 定义构造函数,将生物作为参数:

good_creature(const creature&);

您不知道 society[cop_pos] 是否是正确的类型,因此您无法安全地进行转换。更好的解决方案是使用虚函数创建副本

class creature {
public:
    virtual creature* clone() const = 0;
    ...
};
class good_creature {
public:
    good_creature* clone() { return new good_creature(*this); }
    ...
};
//Similar for bad_creature (and any other derived classes)

在你的情况下,你会这样称呼它: society[pos] = society[cur_pos]->clone();

无需知道您正在克隆的对象的类型。虚函数调用会为您处理这些事情。请注意 good_creature::clone return 是 good_creature* 而不是 creature*。这是一个有效的重载。允许虚函数重载 return 派生 class。在这种情况下,您也可以 return 和 creature*

使用多态和虚拟分派为您完成工作。

在生物中定义一个克隆虚函数class。

class creature
{
    virtual creature * clone() = 0;
}

然后在children中覆盖它:

class good_creature: public creature
{
    virtual creature * clone() override
    {
        return new good_creature(*this);
    }
}

和 bad_creature 类似。

然后使用它:

society[pos] = society[pos - 1]->clone();

旁注:您的设计似乎受到 Java 等语言的影响。这不是(现代)C++ 风格。例如,在现代 C++ 中,所有权更好地表示为 unique_ptr 而不是指针。这将使代码更干净、更安全。