return c++ 中的新构造函数(*this) 工厂
return new constructor(*this) factory in c++
我正在检查 c++ 中的工厂设计模式。我明白了为什么我们使用克隆方法:
Although genetic factories producing clones of the universal soldier
are quite a scary prospect, cloning C objects is a harmless and useful
activity most of the time. Here the goal is slightly different from
what we dealt with so far: We don’t have to create objects from
scratch anymore. We have a pointer to a polymorphic object, and we’d
like to create an exact copy of it. Because we don’t exactly know the
type of the polymorphic object, we don’t exactly know what new object
to create, and this is the actual issue. Quote
我没看懂的部分是函数virtual Line* Clone() const
中的return new Line(*this);
。你能告诉我我的解释是否正确吗
- 在
Shape* Create(const std::string& key) const
中我们调用tmp=((*it).second)->Clone();
- 因为我们想要一个精确的副本,我们在这里所做的就是调用复制构造函数
Line(const Line &)
并将其作为参数传递。但是由于 this
一个指针,我们必须取消引用它,因为我们通过引用传递给复制构造函数。
- 假设我们用
return new Line()
代替return new Line(*this)
会发生什么?我们不会返回对象的副本而是一个新对象?这是一种愚蠢和错误的。为什么要创建一个新对象,因为它已经存在
public:
virtual Shape* Clone() const = 0;
...
};
class Line : public Shape
{
public:
virtual Line* Clone() const
{
return new Line(*this);
}
...
};
//The create function is from the Factory
Shape* Create(const std::string& key) const
{
Figure* tmp=0;
std::map<string, Figure*>::const_iterator it=m_map.find(key);
if(it!=m_map.end())
{
tmp=((*it).second)->Clone();
}
return tmp;
}
表达式 new Line(*this)
创建一个新的 Line
对象并使用 Line
copy-constructor.[=16 构造新对象=]
这应该制作 *this
的精确副本,即您克隆 *this
。
当然,要使这一切正常,请考虑 the rules of three, five and zero。
假设我们使用 return new Line() 而不是 return new Line(*this) 会发生什么?
回答:它将创建使用默认构造函数创建的 Line 的新对象,该构造函数可能与您调用克隆方法所使用的对象不同。所以,如果你想将对象克隆到调用对象,你需要使用复制构造函数创建对象,即使用代码 new Line(*this)
我正在检查 c++ 中的工厂设计模式。我明白了为什么我们使用克隆方法:
Although genetic factories producing clones of the universal soldier are quite a scary prospect, cloning C objects is a harmless and useful activity most of the time. Here the goal is slightly different from what we dealt with so far: We don’t have to create objects from scratch anymore. We have a pointer to a polymorphic object, and we’d like to create an exact copy of it. Because we don’t exactly know the type of the polymorphic object, we don’t exactly know what new object to create, and this is the actual issue. Quote
我没看懂的部分是函数virtual Line* Clone() const
中的return new Line(*this);
。你能告诉我我的解释是否正确吗
- 在
Shape* Create(const std::string& key) const
中我们调用tmp=((*it).second)->Clone();
- 因为我们想要一个精确的副本,我们在这里所做的就是调用复制构造函数
Line(const Line &)
并将其作为参数传递。但是由于this
一个指针,我们必须取消引用它,因为我们通过引用传递给复制构造函数。 - 假设我们用
return new Line()
代替return new Line(*this)
会发生什么?我们不会返回对象的副本而是一个新对象?这是一种愚蠢和错误的。为什么要创建一个新对象,因为它已经存在
public:
virtual Shape* Clone() const = 0;
...
};
class Line : public Shape
{
public:
virtual Line* Clone() const
{
return new Line(*this);
}
...
};
//The create function is from the Factory
Shape* Create(const std::string& key) const
{
Figure* tmp=0;
std::map<string, Figure*>::const_iterator it=m_map.find(key);
if(it!=m_map.end())
{
tmp=((*it).second)->Clone();
}
return tmp;
}
表达式 new Line(*this)
创建一个新的 Line
对象并使用 Line
copy-constructor.[=16 构造新对象=]
这应该制作 *this
的精确副本,即您克隆 *this
。
当然,要使这一切正常,请考虑 the rules of three, five and zero。
假设我们使用 return new Line() 而不是 return new Line(*this) 会发生什么?
回答:它将创建使用默认构造函数创建的 Line 的新对象,该构造函数可能与您调用克隆方法所使用的对象不同。所以,如果你想将对象克隆到调用对象,你需要使用复制构造函数创建对象,即使用代码 new Line(*this)