C ++覆盖方法 [英] C++ Overriding Methods

查看:249
本文介绍了C ++覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这是什么。



我有一个Scene类有一个实体的向量,并允许你添加和获得实体场景:

 类Scene {
private:
// - PRIVATE DATA ---- -
vector< Entity> entityList;
public:
// - STRUCTORS ---------
Scene();
// - PUBLIC METHODS ----
void addEntity(Entity); //添加实体到列表
Entity getEntity(int); //从列表中获取实体
int entityCount();
};

我的实体类如下(输出用于测试):

  class Entity {
public:
virtual void draw(){cout< 否< endl; };
};

然后我有一个Polygon类继承自Entity:

 类多边形:public Entity 
{
private:
// - 私有数据------
vector< Point2D> vertexList; //顶点列表
public:
// - STRUCTORS ---------
Polygon(){}; //默认构造函数
多边形(向量< Point2D>); //通过点声明多边形
// - PUBLIC METHODS ----
int vertexCount(); //返回顶点数量
void addVertex(Point2D); // Add vertex
void draw(){cout< 是< endl; }; // Draw polygon
// - ACCESSORS ---------
Point2D getVertex(int); // Return vertex
};

如你所见,它有一个draw()方法,应该覆盖draw继承自Entity类。



但它不是。使用以下代码时:

  scene-> getEntity(0).draw 

其中实体0是多边形(或至少应为),它从父方法(好像它不是多边形,只是一个实体)。事实上,它似乎不允许我调用任何独特的方法Polygon没有得到:



'一些方法名称':is not Entity的成员



有什么想法吗?



感谢您的帮助。



UPDATE:



所以我实现了第一个答案中给出的代码,我不知道如何将我的多边形添加到列表。这样的东西吗?

  const tr1 :: shared_ptr< Entity> poly = new Polygon; 
poly-> addVertex(Point2D(100,100));
poly-> addVertex(Point2D(100,200));
poly-> addVertex(Point2D(200,200));
poly-> addVertex(Point2D(200,100));
scene-> addEntity(poly);

我只是不习惯这个shared_ptr业务。



你有你的代码,但是你需要发布你的代码。一个具体类多边形从另一个具体类派生 Entity 。您的addEntity和getEntity函数接受并通过值返回 Entity 如果您尝试传入或检索 Entity ,你将只复制该对象的 Entity 部分,并且该对象的派生部分的信息将丢失。 p>

此外,您还有 Entity 向量一个基类对象的向量,所以你没有办法存储除对象的基本类型之外的任何东西。



如果你需要一个混合类型的集合对象,但是都派生自 Entity ,您可能需要使用动态创建的对象和某种智能指针,如 tr1 :: shared_ptr boost :: shared_ptr



例如


b $ b

  class Scene {
private:
// - PRIVATE DATA ------
vector< std :: tr1 :: shared_ptr< Entity> > entityList;
public:
// - STRUCTORS ---------
Scene();
// - PUBLIC METHODS ----
void addEntity(const std :: tr1 :: shared_ptr< Entity>&); //将实体添加到列表
const std :: tr1 :: shared_ptr< Entity> getEntity(int) //从列表中获取实体
int entityCount();
};

编辑



你更新的调用代码基本上是正确的,虽然使用一个共享指针的本地const引用是有点模糊。



我可能会去像: / p>

  std :: tr1 :: shared_ptr< Polygon> poly(new Polygon); 
poly-> addVertex(Point2D(100,100));
poly-> addVertex(Point2D(100,200));
poly-> addVertex(Point2D(200,200));
poly-> addVertex(Point2D(200,100));
scene-> addEntity(poly);


I can't figure out what is up with this.

I have a Scene class that has a vector of Entities and allows you to add and get Entities from the scene:

class Scene {
    private:
        // -- PRIVATE DATA ------
        vector<Entity> entityList;
    public:
        // -- STRUCTORS ---------
        Scene();
        // -- PUBLIC METHODS ----
        void addEntity(Entity); // Add entity to list
        Entity getEntity(int); // Get entity from list
        int entityCount();
};

My Entity class is as follows (output is for testing):

class Entity {
    public:
        virtual void draw() { cout << "No" << endl; };
};

And then I have a Polygon class that inherits from Entity:

class Polygon: public Entity
{
    private:
        // -- PRIVATE DATA ------
        vector<Point2D> vertexList; // List of vertices
    public:
        // -- STRUCTORS ---------
        Polygon() {}; // Default constructor
        Polygon(vector<Point2D>); // Declare polygon by points
        // -- PUBLIC METHODS ----
        int vertexCount(); // Return number of vertices
        void addVertex(Point2D); // Add vertex
        void draw() { cout << "Yes" << endl; }; // Draw polygon
        // -- ACCESSORS ---------
        Point2D getVertex(int); // Return vertex
};

As you can see, it has a draw() method that should override the draw() method it inherits from the Entity class.

But it doesn't. When using the following code:

scene->getEntity(0).draw();

where entity 0 is a Polygon (or at least should be), it prints "No" from the parent method (as though it's not a Polygon, just an Entity). In fact, it doesn't seem to let me call any methods unique to Polygon without getting:

'some method name' : is not a member of 'Entity'

So any idea what's up?

Thanks for the help.

UPDATE:

So I've implemented the code given in the first answer, but I'm not sure how to add my polygon to the list. Something like this?

const tr1::shared_ptr<Entity>& poly = new Polygon;
poly->addVertex(Point2D(100,100));
poly->addVertex(Point2D(100,200));
poly->addVertex(Point2D(200,200));
poly->addVertex(Point2D(200,100));
scene->addEntity(poly);

I'm just not used to this shared_ptr business.

解决方案

I think that you need to post your calling code, but the essentially problem is this.

You have a concrete class Polygon deriving from another concrete class Entity. Your addEntity and getEntity functions take and return an Entity by value so if you try to pass in or retrieve an Entity, you will copy only the Entity part of that object (slicing it) and the information about the derived part of the object will be lost.

In addition you have a vector of Entity, which is a vector of base class objects, so you have no way of storing anything other than the base type of object.

If you need to have a collection of a mixed type of objects, but all derived from Entity, you may need to use dynamically created objects and some sort of smart pointer such as a tr1::shared_ptr or a boost::shared_ptr.

E.g.

class Scene {
    private:
        // -- PRIVATE DATA ------
        vector< std::tr1::shared_ptr<Entity> > entityList;
    public:
        // -- STRUCTORS ---------
        Scene();
        // -- PUBLIC METHODS ----
        void addEntity( const std::tr1::shared_ptr<Entity>& ); // Add entity to list
        const std::tr1::shared_ptr<Entity> getEntity(int); // Get entity from list
        int entityCount();
};

Edit

Your updated calling code is essentially correct, although using a local const reference to a shared pointer is a bit obscure.

I'd probably go with something like:

std::tr1::shared_ptr<Polygon> poly( new Polygon );
poly->addVertex(Point2D(100,100));
poly->addVertex(Point2D(100,200));
poly->addVertex(Point2D(200,200));
poly->addVertex(Point2D(200,100));
scene->addEntity(poly);

这篇关于C ++覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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