[英]Copy constructor call C++
So I am quite confused about copy constructors in C++. 因此,我对C ++中的复制构造函数感到困惑。 I have the following code: 我有以下代码:
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
}
So I have an abstract class called creature
, a good_creature
and a bad_creature
which are a children class of creature
. 所以我有一个抽象类,称为creature
,一个good_creature
和bad_creature
是creature
的子类。
At my program I also have an array called society
which has type of creature*
objects. 在我的程序中,我还拥有一个名为society
的数组,其中包含creature*
对象的类型。 If my creature through a condition is defined as good, I allocate space for it and store it in society
array as good_creature
. 如果通过条件将我的生物定义为良,我会为其分配空间,并将其存储在society
数组中为good_creature
。 The same happens for bad creature. 坏生物也是如此。 I construct it as described in the following code: 我按照以下代码中的说明构造它:
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);
}
So I have to make a pure virtual function: creature::clone(int position)
which if it's either a good_creature
or a bad_creature
, it has to delete the society[pos]
and make a copy of the society[pos-1]
through a copy constructor . 因此,我必须创建一个纯虚函数: creature::clone(int position)
,如果它是good_creature
或bad_creature
,则必须删除bad_creature
society[pos]
并通过以下方式复制该bad_creature
society[pos-1]
复制构造函数 。
So for example my good_creature::clone(int position)
is like this: 例如,我的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] );
//....
}
I get an error because society[cop_pos]
is of type creature*
. 我得到一个错误,因为society[cop_pos]
是类型society[cop_pos]
creature*
。 I tried casting it to good creature but unfortunately I keep getting errors. 我尝试将其投放到好生物上,但不幸的是,我不断出错。 Is it because I am not calling the copy constructor right, is it because I am not casting right? 是因为我没有正确调用复制构造函数,还是因为我没有正确转换? Any ideas? 有任何想法吗? This has been buffling me for 2 days. 这让我感到疲倦了2天。 Keep in mind I' ma newbie and might have done something wrong. 请记住,我是新手,可能做错了什么。
Also I don't need to define my own copy constructor since all the elements in society[i]
point at the same object that is defined by creature_society * cs
, so I'm trying to use the default constructors since I do not need deep copy . 另外,我不需要定义自己的副本构造函数,因为society[i]
所有元素都指向由creature_society * cs
定义的同一对象,所以我尝试使用默认构造函数,因为我不需要深入复制 。
Thanks for your time. 谢谢你的时间。
UPDATE 更新
A class I forgot to mention and the way I construct society 我忘记提及的课程以及我建立社会的方式
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();
};
You don't know if society[cop_pos]
is the correct type, so you cannot safely cast. 您不知道society[cop_pos]
是否是正确的类型,因此您不能安全地进行转换。 A better solution is to use a virtual function to create a copy 更好的解决方案是使用虚函数创建副本
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)
In your case you'd call it like this: society[pos] = society[cur_pos]->clone();
在您的情况下,您可以这样称呼: society[pos] = society[cur_pos]->clone();
There's no need to know the type of the object you're cloning. 无需知道您要克隆的对象的类型。 The virtual function call takes care of that for you. 虚函数调用将为您解决这一问题。 Note that good_creature::clone
returns a good_creature*
instead of a creature*
. 请注意, good_creature::clone
返回的是good_creature*
而不是creature*
。 This is a valid overload. 这是有效的重载。 A virtual function overload is allowed to return a derived class. 允许虚函数重载返回派生类。 In this case you could have it return a creature*
as well. 在这种情况下,您也可以让它返回一个creature*
。
Use polymorphism and virtual dispatch to do the work for you. 使用多态和虚拟调度为您完成工作。
Define a clone virtual function in creature class. 在生物类中定义一个克隆虚拟函数。
class creature
{
virtual creature * clone() = 0;
}
and then override it in children: 然后在子级中覆盖它:
class good_creature: public creature
{
virtual creature * clone() override
{
return new good_creature(*this);
}
}
and similar for bad_creature. 与bad_creature类似。
Then use it: 然后使用它:
society[pos] = society[pos - 1]->clone();
Side note: your design seems to influenced by languages like Java. 旁注:您的设计似乎受Java之类的语言的影响。 This is not a (modern) C++-style. 这不是(现代)C ++样式。 For example, in modern C++ ownership is better expressed by unique_ptr
instead of pointers. 例如,在现代C ++中,所有权最好用unique_ptr
而不是指针来表示。 This would make code cleaner and much safer. 这将使代码更清洁,更安全。
The problem is that society
is an array of creature
, not of good creature
, so the copy constructor doesn't apply. 问题在于society
是一个creature
数组,而不是good creature
数组,因此复制构造函数不适用。
You can define a constructor for good_creature
and for bad_creature
taking as argument a creature: 您可以使用生物作为参数来为good_creature
和bad_creature
定义构造函数:
good_creature(const creature&);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.