简体   繁体   English

如何访问不在基 class 中的派生类中的 STL 类的成员函数? (正文中的详细解释)

[英]How can I access member functions of STL classes inside derived classes that aren't in the base class? (detailed explanation in body)

Right now I have a base class, class Base{}, with two classes deriving from it, BFS{} and DFS{}.现在我有一个基础 class、class Base{},并从它派生了两个类,BFS{} 和 DFS{}。 BFS has queue, and DFS has stack, so they both have a member called "nodes", but the type is their respective std::queue and std::stack. BFS 有队列,DFS 有堆栈,所以它们都有一个名为“nodes”的成员,但类型是它们各自的 std::queue 和 std::stack。 My search function takes in a pointer to base class as its parameter so that it can accept both derived classes, and runs the search by pushing and popping from the member classes inside the derived classes (as per the usual DFS BFS algorithms).我的搜索 function 将指向基础 class 的指针作为其参数,以便它可以接受两个派生类,并通过从派生类内部的成员类中推送和弹出来运行搜索(根据通常的 DFS BFS 算法)。 The issue is, since I passed in my base class as the parameter, whenever I try to call push or pop on the member stack/queue called "nodes" from the derived classes, it always says that the push/pop cannot be done because there is no member inside the base class called "nodes".问题是,由于我传入了我的基本 class 作为参数,每当我尝试从派生类中调用名为“节点”的成员堆栈/队列上的推送或弹出时,它总是说无法完成推送/弹出,因为基础 class 内部没有称为“节点”的成员。 How am I supposed to make this work?我该怎么做? Also, this setup is a requirement of the assignment I am doing and I just can't figure out how this is supposed to work, any help is appreciated.此外,此设置是我正在执行的任务的要求,我只是无法弄清楚这应该如何工作,感谢任何帮助。 Thanks!谢谢!

class Base {
public:
    virtual void push(uint64_t roomID, float intensity, int distance) = 0;
    virtual Node pop(void) = 0;
    virtual int size(void) = 0;
};

class Breadth : public Base {
public:
    std::queue<std::pair<uint64_t, int>> U;
    void push(uint64_t roomID, float intensity, int distance) { std::pair<uint64_t, int> p(roomID, distance); U.push(p); }
    Node pop() { Node rr; rr.ID = U.front().first; rr.distance = U.front().second;  U.pop(); return rr; }
    int size() { return U.size(); }
};

class Depth : public Base {
public:
    std::stack<std::pair<uint64_t, int>> U;
    void push(uint64_t roomID, float intensity, int distance) { std::pair<uint64_t, int> p(roomID, distance); U.push(p); }
    UnexploredRoom pop() { U.pop(); }
    int size() { U.size(); }
};

void robotSearch::searchLoop(Base* search, Discovered* D, uint64_t roomID)
{
    Node room;
    room.ID = roomID;
    room.distance = 0;
    search->U.push(room); //problem here, compiler wont let me push U
    ...
}

To implement custom behaviour through a pointer to a base class, you need to use virtual functions .要通过指向基础 class 的指针来实现自定义行为,您需要使用虚函数 Another approach would be to use generic code with templates.另一种方法是使用带有模板的通用代码。

Example:例子:

class Base {
public:
    virtual ~Base() {}
    virtual void push(int i) = 0;
    virtual int pop() = 0;
};

class DFS : public Base{
public:
    virtual void push(int i) override { /*...*/ }
    virtual int pop() override { /*...*/ return {}; }
};

class BFS : public Base {
public:
    virtual void push(int i) override { /*...*/ }
    virtual int pop() override { /*...*/ return {}; }
};

Right now, you have some virtual methods push and pop , but for some reason, you don't use them and instead try to access a member of the derived classes instead.现在,您有一些虚拟方法pushpop ,但由于某种原因,您不使用它们,而是尝试访问派生类的成员。 You seem to have copied code from the answer by Ayjay but not applied it correctly.您似乎从 Ayjay 的答案中复制了代码,但没有正确应用它。
That member U should really not be exposed like this, that is, it should be private , and you should use your class methods to manipulate it.那个成员U真的不应该这样暴露,也就是说,它应该是private ,你应该使用你的 class 方法来操作它。

Therefore, you wouldn't write因此,你不会写

search->U.push(room);

even if it was legal here (which it isn't, as the base class does not have anything named like that).即使它在这里合法的(它不是,因为基础 class 没有任何类似的名称)。
Instead, you go with相反,你 go 与

search->push(room);

Note that I omitted the other arguments that this takes, of course you also have to provide values for your intensity and distance arguments.请注意,我省略了其他需要的 arguments,当然您还必须提供intensitydistance arguments 的值。

Doing so will call the appropriate method, that is either Breadth::push or Depth::push , which then will access the corresponding member of the respective class.这样做将调用适当的方法,即Breadth::pushDepth::push ,然后将访问相应 class 的相应成员。

By the way, for reasons of control, you should use the override keyword as Ayjay did, and also, you should give a member a more descriptive name that U .顺便说一句,出于控制的原因,您应该像 Ayjay 一样使用override关键字,而且,您应该给成员一个比U更具描述性的名称。

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

相关问题 抽象类成员无法访问派生类中设置的值 - Abstract class member can't access value set in derived classes 为什么不能将多态派生类嵌套在基类中? - Why can't polymorphic derived classes be nested inside the base class? 用多个派生类调用基类成员函数 - Calling base class member functions with multiple derived classes 多态C ++,尝试访问从抽象基类派生的类的成员函数 - polymorphism c++, trying to access member functions of classes that are derived from abstract base class 仅授予某些派生类访问基类中的成员函数的权限 - Give only some derived classes access to member functions from base class 从派生类访问成员函数的基类指针向量? 正确的方法? - Vector of base class pointers to access member functions from derived classes? The correct way? 如何通过类型为base的指针访问派生类中的成员,但指向派生类 - How to access member in derived classes by a pointer of type base but pointing to the derived class 在基类和派生类中初始化类成员变量 - initialization of class member variables in base and derived classes 我们可以在派生类中访问基类的受保护成员函数吗? - Can we access protected member functions of base class in derived class? 获取派生类以不继承基类函数 - Getting derived classes to not inherit base class functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM