观察者模式和继承:没有调用正确的函数

Observer pattern and inheritance: Not calling the correct function

我正在尝试为我为学校项目创建的游戏实现观察者模式。

我创建了 2 个虚拟 classes,Observer 和 Observable。

Observer.h:

#ifndef OBSERVER_H
#define OBSERVER_H
#include <vector>

class Observable;

class Observer
{
    public:
     Observer();
     virtual ~Observer();
     virtual void update(Observable* ob) =0;
};

#endif

Observer.cpp:

 #include "stdafx.h"
 #include "Observer.h"


 Observer::Observer()
 {
 }

 Observer::~Observer()
 {
 }

Observable.h:

#ifndef OBSERVEABLE_H
#define OBSERVEABLE_H
#include <vector>
#include "Observer.h"


class Observable
{
    protected:
    std::vector<Observer*> observers;
    public:
    Observable();
    virtual ~Observable();
    virtual void attach(Observer *a);
    virtual void detach(Observer *a);
    virtual void notify();
};

#endif

Observable.cpp:

#include "stdafx.h"
#include "Observable.h"

Observable::Observable()
{
}

Observable::~Observable()
{
}

void Observable::attach(Observer *a)
{
    observers.push_back(a);
}

void Observable::detach(Observer *a)
{
     for (auto it = this->observers.begin(); it < this->observers.end(); it++) 
     {

            if (*it == a) 
            {
            this->observers.erase(it);
            break;
            }
     }
 }

void Observable::notify()
{
    for (int i = 0; i < observers.size(); i++)
        observers[i]->update(this);
}

我有一个继承自Observable的Mapclass和一个继承Observer的mapViewclass(Map很长,我只包含了相关的功能)

Map.h:

#ifndef MAP_H
#define MAP_H
#include "Observable.h"
#include <iostream>

class Map : public Observable
{
    public:
    Map();
    ~Map();
    void getLatest();
    void notify();
};

#endif

Map.cpp:

#include "stdafx.h"
#include "Map.h"

Map::Map()
{
}

Map::~Map()
{
}

void Map::getLatest()
{
    using namespace std;
    cout << "This is the latest info!" << endl;
}

mapView.h:

#ifndef MAP_V_H
#define MAP_V_H
#include "Observer.h"
#include "Map.h"
#include "Plants.h"

class mapView : public Observer
{
    public:
    mapView();
    ~mapView();
    void update(Map* map);
};

#endif

mapView.cpp:

#include "stdafx.h"
#include "mapView.h"
#include "Map.h"

mapView::mapView()
{
}

mapView::~mapView()
{
}

void mapView::update(Map* map)
{
    map->getLatest();
}

最后,我的 main 只是创建一个 Map 和一个 mapView,附加 mapView,然后调用 map.notify()

main.cpp:

#include "stdafx.h"
#include "setUp.h"
#include "Map.h"
#include "mapView.h"

int main()
{
    Map gameMap;
    mapView view;
    gameMap.attach(&view);

    gameMap.notify();

    return 0;
}

我运行这里进了一些问题。我无法创建 mapView 项目,因为编译器说我从未实现更新的覆盖版本(Observable* ob)....我尝试使用 update(Map* map)但看起来尽管 Map 继承自 Observable,但它好像不算同一个签名,所以编译不过。

我试图更改我的 mapView::update() 函数以取一个指向 Observable 的指针,但这不起作用,因为该函数从 Map class.

调用了一些东西

然后我尝试将更新函数更改为不是虚函数(在虚拟 class 中实现为空),但似乎每次我尝试传递 Map 进行更新时,它都会调用base class 函数而不是 mapView 版本。换句话说,永远不会调用 getLatest()。

我现在很困惑,因为这与我认为多态性的工作方式背道而驰。如果可能的话,将不胜感激!

谢谢,

您的基地 class 声明:

virtual void update(Observable* ob) =0;

您导出 class 声明:

void update(Map* map);

这些不是同一个签名。如果您使用新的 override 关键字,您会在编译时看到您实际上并没有覆盖虚拟方法。

如果你知道你只会得到Maps,那么你可以直接使用static_cast。但是使用 dynamic_cast:

更安全
void update(Observable* o) override { // now we're ok
    if (auto map = dynamic_cast<Map*>(o)) {
        // okay, got a Map
        // ....
    }
    else {
        // huh?
    }
}

超简型理论题外话。覆盖的典型规则是 covariant in return 和 contravariant in the argument type。您可以指定更派生的 return 类型,或更基本的参数类型。以这种方式思考 - 如果你有一个基础 class 函数接受并 returning 一个 Car*... 你的论点可以是一个 Car* (这正是预期的) ,或者它可以是 Vehicle*(因为任何你可以用 Vehicle 做的事情,你可以用 Car 做 - 这仍然有效),但它不能是 SportsCar* (因为调用者可能会传递给你一个 Car 而不是 SportsCar 并且有理由期望它起作用!)派生的 class 接受是没有意义的只有 Maps - 你必须能够接受任何 Observables,甚至不能接受 Maps!