Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中列表 >与列表的区别_C++_Pointers_Design Patterns - Fatal编程技术网

C++中列表 >与列表的区别

C++中列表 >与列表的区别,c++,pointers,design-patterns,C++,Pointers,Design Patterns,编辑:问题已经回答了,谢谢!此外,此代码也是Observer模式的一部分。如果您知道模式,您将理解此代码。谢谢: 我正在学习观察者模式,但我不理解提供的代码中的额外*部分。 Subject.h有这个代码 //Subject.h #pragma once #include<list> #include "Observable.h" using namespace std; class Subject : public Observable { public: Subjec

编辑:问题已经回答了,谢谢!此外,此代码也是Observer模式的一部分。如果您知道模式,您将理解此代码。谢谢:

我正在学习观察者模式,但我不理解提供的代码中的额外*部分。 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 *
};
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

这与int和int*之间的区别相同。不,它是指向指向观察者的指针列表的指针。@Mrtnchps list将是指向指针的指针列表[OT]这似乎是一个奇怪的设计。为了使@Barry的观点更有力:发布的代码是胡说八道。无论从哪里来,把它放回去,永远不要再去那里。