在下面的代码中,我编写了一个工厂方法来创建类heirachy中的类型的对象。
#include <iostream>
#include <memory>
using namespace std;
enum Type {
_Base, _A, _B, _C
};
class Base{
private:
Type type = _Base;
public:
virtual Type getType(){
return type;
}};
class A : public Base{
private:
Type type = _A;
public:
using Base::Base;
};
class B : public Base{
private:
Type type = _B;
public:
using Base::Base;
};
class C : public Base{
private:
Type type = _C;
public:
using Base::Base;
};
shared_ptr<Base> letterFactory(Type which){
shared_ptr<Base> base = make_unique<Base>(Base());
switch (which){
case _A:
base = make_unique<Base>(A());
case _B:
base = make_unique<Base>(A());
case _C:
base = make_unique<Base>(C());
}
return base;
}
int main(){
shared_ptr<Base> instanceOfA = letterFactory(_A);
cout << instanceOfA->getType() << endl;
shared_ptr<Base> instanceOfB = letterFactory(_B);
cout << instanceOfB->getType() << endl;
shared_ptr<Base> instanceOfC = letterFactory(_C);
cout << instanceOfC->getType() << endl;
return 0;
};
输出是
0
0
0
我怎样才能输出
1
2
3
最佳答案
您的Base
类具有一个成员type
和一个虚拟成员函数getType()
,该函数返回成员type
的值。您的类A
,B
和C
源自Base
。这意味着它们都有一个Base
子对象。该子对象包含成员Base::type
。此外,它们都还添加了另一个成员type
,该成员后来从未被任何人使用。同样,它们都没有覆盖getType
方法。所以每当你打电话
instanceOfX->getType()
即使
instanceOfX
指向其中一个派生类的实例,由于没有一个派生类会覆盖getType
,您最终将调用Base::getType
,它将返回Base::type
的值,该值始终为_Base
…您实际想要的可能是以下几种:
struct Base
{
virtual Type getType() const = 0;
protected:
Base() = default;
Base(Base&&) = default;
Base(const Base&) = default;
Base& operator =(Base&&) = default;
Base& operator =(const Base&) = default;
~Base() = default;
};
class A : public Base
{
public:
Type getType() const override { return _A; }
};
class B : public Base
{
public:
Type getType() const override { return _B; }
};
class C : public Base
{
public:
Type getType() const override { return _C; }
};
请注意,这几乎肯定是错误的设计。这样的
getType
方法可以服务的唯一目的是使客户端代码可以找到它所获得的Base*
指向的对象的具体类型。如果您需要此信息,则您的设计违反了Liskov Substitution principle…除此之外,请注意
_Base
,_A
,_B
和_C
是保留名称[lex.name]/3,您不应该在C++代码中使用…
关于c++ - 此工厂功能有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59662257/