我正在学习观察者模式,我不明白提供的代码中的额外*。 Subject.h有这段代码
//Subject.h
#pragma once
#include<list>
#include "Observable.h"
using namespace std;
class Subject : public Observable {
public:
Subject();
~Subject();
void attach(Observer *);
void detach(Observer *);
void notify();
private:
list<Observer*>* _observers; // ----------Here there is two *
};
Obervable.h有这个代码(实现Observer.h ...)
//Observable.h
#pragma once
#include <list>
using namespace std;
class Observer;
class Observable
{
public:
virtual ~Observable() {};
virtual void attach(Observer *) = 0;
virtual void detach(Observer *) = 0;
virtual void notify() = 0;
protected:
Observable() {};
private:
list<Observer*> _observers; // ----------Here there is only one *
};
答案 0 :(得分:1)
list 是标准库提供的众多类之一。
它的行为与任何其他类类型相同:
int : integer
int* : pointer to integer
Observer : Observer instance
Observer* : pointer to Observer instance
list<Observer> : list of Observers
list<Observer*>* : pointer to list of pointers to Observers