简体   繁体   English

通过基类指针获取派生成员变量

[英]Getting derived member variable through base class pointer

consider this base class: 考虑这个基类:

struct drawable 
{
    virtual void draw(sf::RenderWindow &window) const = 0;

};

and this derived class: 这个派生类:

struct rectangle : drawable 
{
  rectangle(sf::Vector2f pos, sf::Vector2f size);
  void draw(sf::RenderWindow &window) const;
  sf::RectangleShape body;
};

I have similar derived classes for other shapes like circle, line and triangle. 对于其他形状(如圆形,直线形和三角形),我也有类似的派生类。 I use this function to return shapes based on a string of text I get from a file: 我使用此函数根据从文件中获得的文本字符串返回形状:

drawable * string_to_object(std::string name)
{
    if (name == "RECTANGLE")
    {
        return new rectangle(sf::Vector2f(20,20), sf::Vector2f(5,5));
    }
    else if (name == "BALL")
    {
        return new ball(sf::Vector2f(10,10), 5, sf::Vector2f(0,0));
    }
    else if (name == "LINE")
    {
        return new line(sf::Vector2f(30,30), 10, 5);
    }
}

Now in my main I have variables like this to test if it works: 现在在我的主目录中,我有这样的变量来测试它是否有效:

auto game_object = string_to_object("BALL");

Now the problem is I need to perform actions/checks on the shape's body, which is a member of the derived class that I cannot access from the drawable pointer variables. 现在的问题是,我需要在形状主体上执行操作/检查,这是我无法从可绘制指针变量访问的派生类的成员。 It's also a problem that the type of the body is not set, it can be a RectangleShape, CircleShape etc so a getBody() function would need a variable return type. 还有一个问题是没有设置主体的类型,它可以是RectangleShape,CircleShape等,因此getBody()函数将需要一个可变的返回类型。 How would I go about getting access to the body in a generic way? 我将如何以一般方式进入身体? I've tried templates but I realized that won't work since it's a runtime problem. 我已经尝试过模板,但是我意识到这是无法解决的,因为这是一个运行时问题。

If I understand your question correctly, there are multiple ways how to solve this issue. 如果我正确理解了您的问题,则有多种方法可以解决此问题。

  1. Re-think your architecture. 重新考虑您的架构。 You could introduce other virtual functions to drawable that every subclass implements. 您可以向每个子类实现的可绘制对象引入其他虚函数。 In these functions you'd implement all the checks/actions you need. 在这些功能中,您将实现所需的所有检查/操作。 Since they are implemented in the base class, they have access to the shape's body and since it is a virtual function of the base you can call these functions from the outside. 由于它们是在基类中实现的,因此可以访问形状的主体,并且由于它是基类的虚函数,因此您可以从外部调用这些函数。

  2. Since your drawable object has a virtual function, you could use RTTI to check the type at runtime and perform a dynamic_cast See: https://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI 由于您的可绘制对象具有虚拟功能,因此可以使用RTTI在运行时检查类型并执行dynamic_cast,请参见: https : //en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI

I'd prefer the first option whenever you can. 只要有可能,我都希望第一种选择。

It sounds like you're having trouble deciding what functionality is generic to all drawable objects, and what is specific to rectangle , ball and so on. 听起来您很难确定所有drawable对象的通用功能,以及rectangleball等的通用功能。 Attributes and methods that apply to all drawable objects can be declared within drawable , but anything that only applies to a particular kind of drawable (like the width and height of rectangle vs. the radius of a ball ) go in the derived classes. 可以在drawable声明适用于所有drawable对象的属性和方法,但是仅适用于特定类型的drawable drawable对象的任何内容(例如rectangle的宽度和高度与ball的半径)都在派生类中。

In your example, each of the derived classes must implement the draw method if you want to instantiate them (because it is declared pure virtual in the base drawable class). 在您的示例中,如果要实例化它们,则每个派生类都必须实现draw方法(因为在基本drawable类中将其声明为纯虚函数)。 Each of these specific derived implementations can access the specific attributes of the derived class. 这些特定的派生实现中的每一个都可以访问派生类的特定属性。 So the rectangle::draw method can access the width and height, while the ball::draw method can access the radius. 因此, rectangle::draw方法可以访问宽度和高度,而ball::draw方法可以访问半径。

Then, when you have a collection of pointers to drawable objects (which are really instances of the derived classes) you can simply call the draw method for each of them. 然后,当您有一组指向可drawable对象的指针(实际上是派生类的实例)时,您可以简单地为每个对象调用draw方法。

Sorry if this seems overly simplistic - I hope it's clear. 抱歉,这似乎过于简单了-我希望这很清楚。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指向派生 class 中的基 class 作为成员变量的指针 - Pointer to base class in derived class as a member variable 如何通过基指针访问派生 class 的成员函数? - How to access member functions of a derived class through a base pointer? 将指向派生类成员变量的指针转换为指向基类成员变量的指针是否合法? - Is it legal to cast a pointer-to-derived-class-member-variable to a pointer-to-base-class-member-variable? 通过基指针获取派生类? - getting derived class by base pointer? 无法将“指向派生类的成员指针”转换为“指向基类的成员指针” - Cannot cast “member pointer to derived class” to “member pointer to base class” 为什么派生类不能通过指向基类的指针访问其基类的受保护成员? - Why can a derived class not access a protected member of its base class through a pointer to base? 具有派生成员的基类的唯一指针 - unique pointer of base class with derived member 基类指针仅获得派生类变量值,而不得到基类变量值,为什么? - Base Class Pointer only getting a Derived class variable value and not the base class variable value why? 尝试通过基类指针访问派生类成员函数将导致哪种错误? - What kind of error will result from attempting to access a derived class member function through a base class pointer? 派生类不能使用成员指针来保护基类成员 - Derived class cannot use member pointer to protected base class member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM