【发布时间】:2016-03-15 01:28:25
【问题描述】:
我正在尝试为我为学校项目创建的游戏实现观察者模式。
我创建了 2 个虚拟类,Observer 和 Observable。
观察者.h:
#ifndef OBSERVER_H
#define OBSERVER_H
#include <vector>
class Observable;
class Observer
{
public:
Observer();
virtual ~Observer();
virtual void update(Observable* ob) =0;
};
#endif
观察者.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的Map类,还有一个继承自Observer的mapView类(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 项目,因为编译器说我从未实现过 update(Observable* ob)....好像不算同一个签名,所以不会编译。
我试图更改我的 mapView::update() 函数以获取指向 Observable 的指针,但这不起作用,因为该函数从 Map 类中调用了一些东西。
然后我尝试将更新函数更改为不是虚拟函数(虚拟类中的实现为空),但似乎任何时候我尝试传递 Map 进行更新,它都会调用基类函数而不是地图视图版本。换句话说,永远不会调用 getLatest()。
我现在很困惑,因为这与我认为多态性的工作方式背道而驰。如果可能的话,希望能得到一些帮助或见解!
谢谢,
【问题讨论】:
-
模板是你的朋友
标签: c++ inheritance observer-pattern