如何有条件地实例化不同的子类? [英] How to instantiate different child classes conditionally?

查看:133
本文介绍了如何有条件地实例化不同的子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在main函数中,我想获取用户的输入。根据输入,我将创建 Rectangle Circle ,它们是<$ c $的子类C>对象。如果没有输入(或未知),那么我将只创建一个通用对象。

For example, in the main function, I want to get the user's input. Depending on the input, I will create either a Rectangle or a Circle, which are child classes of Object. If there's no input (or unknown), then I will just create a generic object.

class Object
{ 
       public:
           Object();
           void Draw();
       private:
           ....  
};
class Rectangle:public Object
{ 
       public:
           Rectangle();
           .... //it might have some additional functions
       private:
           ....  
};

class Circle:public Object
{ 
       public:
           Circle();
           .... //it might have some additional functions
       private:
           ....  
};

主要功能:

string objType;
getline(cin, objType);

if (!objType.compare("Rectangle"))
     Rectangle obj;
else if (!objType.compare("Circle"))
     Circle obj;
else 
     Object obj;

obj.Draw();

当然,上面的代码不起作用,因为我无法在If中实例化一个对象声明。所以我试过这样的事情。

Of course, the code above won't work because I can't instantiate an object inside an If statement. So i tried something like this.

Object obj;
if (!objType.compare("Rectangle"))
    obj = Rectangle();
else if (!objType.compare("Circle"))
    obj = Circle();


obj.Draw();

这段代码会编译,但它不会做我想要的。由于某种原因,对象不是以子类的方式启动的(例如,我在子类中设置了一些Object的成员变量,特别是向量)。但是,当我在Child类构造函数中放置一个断点时,它确实在那里运行。

This code would compile, but it won't do what I want. For some reason, the object was not initiated the way the child class should (for example, I set the some Object's member variables, specifically, a vector, differently in the child classes). However, when I put a break point at the Child class constructor, it did run through there.

那么我应该如何在一些if语句中将实例化对象作为其子类?

So how should I put instantiate Objects as its child classes in some if-statements??

推荐答案

可以中创建自动对象,如果语句,但它们将在范围的末尾被销毁因此他们不能解决这个问题。

You can create automatic objects in if statements, but they will be destroyed at the end of the scope they are created in so they don't work for this problem.

你不能做 obj = Rectangle()一个是因为切片

你必须有一个指向对象的指针。指向基础对象的指针也可以指向子对象的实例。然后,您可以在中动态创建对象,如果,则使用 new (使用 new <创建的对象/ code>忽略范围,仅在指向它们的指针上调用 delete 时销毁,然后 delete 当你完成时:

You have to have a pointer to an Object. Pointers to base objects can also point to instances of child objects. Then you can dynamically create the object inside the if with new (objects created with new disregard scope and are only destroyed when you call delete on a pointer to them), then delete it when you're done:

Object* obj = NULL; // obj doesn't point to anything yet
string objType;
getline(cin, objType);

if (objType == "Rectangle")
    obj = new Rectangle; // make obj point to a dynamic Rectangle
else if (objType == "Circle")
    obj = new Circle; // make obj point to a dynamic Circle
else
    obj = new Object;  // make obj point to a dynamic Object

obj->Draw(); // draw whatever shape obj really points to

delete obj; // deallocate dynamic object

或者,您可以使用智能指针然后您不必担心关于手动释放对象:

Alternatively, you can use smart pointers and then you don't have to worry about manually deallocating the object:

std::unique_ptr<Object> obj(NULL); // obj doesn't point to anything yet
string objType;
getline(cin, objType);

if (objType == "Rectangle")
    obj.reset(new Rectangle); // make obj point to a dynamic Rectangle
else if (objType == "Circle")
    obj.reset(new Circle); // make obj point to a dynamic Circle
else
    obj.reset(new Object);  // make obj point to a dynamic Object

obj->Draw(); // draw whatever shape obj really points to

// the unique_ptr takes care of delete'ing the object for us
// when it goes out of scope

这篇关于如何有条件地实例化不同的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆