使用基类指针访问派生类成员 [英] Accessing derived class members with a base class pointer

查看:407
本文介绍了使用基类指针访问派生类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中创建一个简单的控制台游戏



我想知道如果我可以从'entPlayer'类访问成员,到基类('Entity'):

  class Entity {
public:
void setId int id){Id = id; }
int getId(){return Id; }
protected:
int Id;
};

class entPlayer:public Entity {
string Name;
public:
void setName(string name){Name = name; }
string getName(){return Name; }
};

Entity * createEntity(string Type){
Entity * Ent = NULL;
if(Type ==player){
Ent = new entPlayer;
}
return Ent;
}

void main(){
Entity * ply = createEntity(player);
ply-> setName(Test);
ply-> setId(1);

cout<< ply-> getName()<< endl
cout<< ply-> getId()<< endl

delete ply;
}

如何调用ply-> setName等? >



如果这样不可能,更好的方法是什么?

解决方案

可以使用转换。如果你知道一个事实,基类指针指向一个派生类的对象,你可以使用 static_cast

  Entity * e = / *指向entPlayer对象的指针* /; 
entPlayer * p = static_cast< entPlayer *>(e);
p-> setName(Test);

如果你不确定,那么你需要使用 dynamic_cast 并测试结果,以确定它不为null。请注意,如果基类至少有一个虚函数,则只能使用 dynamic_cast 。例如:

 实体* e = / *指向某个实体的指针* /; 
entPlayer * p = dynamic_cast< entPlayer *>(e);
if(p)
{
p-> setName(Test);
}

也就是说,使用多态



说到虚函数,你的类层次结构作为实现具有未定义的行为:你只能通过指向一个派生类型的指针来删除一个派生类型的对象。其基类如果基类为虚拟析构函数。因此,您需要向基类添加一个虚拟析构函数。


I am making a simple console game in C++

I would like to know if I can access members from the 'entPlayer' class while using a pointer that is pointing to the base class ( 'Entity' ):

class Entity {
public:
    void setId(int id) { Id = id; }
    int getId() { return Id; }
protected:
    int Id;
};

class entPlayer : public Entity {
    string Name;
public:
    void setName(string name) { Name = name; }
    string getName() { return Name; }
};

Entity *createEntity(string Type) {
    Entity *Ent = NULL;
    if (Type == "player") {
        Ent = new entPlayer;
    }
    return Ent;
}

void main() {
    Entity *ply = createEntity("player");
    ply->setName("Test");
    ply->setId(1);

    cout << ply->getName() << endl;
    cout << ply->getId() << endl;

    delete ply;
}

How would I be able to call ply->setName etc?

OR

If it's not possible that way, what would be a better way?

解决方案

It is possible by using a cast. If you know for a fact that the base class pointer points to an object of the derived class, you can use static_cast:

Entity* e = /* a pointer to an entPlayer object */;
entPlayer* p = static_cast<entPlayer*>(e);
p->setName("Test");

If you don't know for sure, then you need to use dynamic_cast and test the result to see that it is not null. Note that you can only use dynamic_cast if the base class has at least one virtual function. An example:

Entity* e = /* a pointer to some entity */;
entPlayer* p = dynamic_cast<entPlayer*>(e);
if (p)
{
    p->setName("Test");
}

That said, it would be far better to encapsulate your class's functionality using polymorphism (i.e. virtual functions).

Speaking of virtual functions, your class hierarchy as implement has undefined behavior: you can only delete an object of a derived type through a pointer to one of its base classes if the base class as a virtual destructor. So, you need to add a virtual destructor to the base class.

这篇关于使用基类指针访问派生类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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