Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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++;不同行为的花括号_C++ - Fatal编程技术网

C++ c++;不同行为的花括号

C++ c++;不同行为的花括号,c++,C++,我很困惑。 为什么这两个主要函数会产生不同的结果(cygwing++)。 似乎括号2次就产生了PinDisplay 我以为 声明1 声明2 声明3 完全一样 我以为 声明1 { 声明2; } 声明3 整个代码: 观察员h #ifndef Observer_Pattern_Framework__ #define Observer_Pattern_Framework__ // ==== Observer Pattern Framework ===========================

我很困惑。 为什么这两个主要函数会产生不同的结果(cygwing++)。 似乎括号2次就产生了PinDisplay

我以为
声明1
声明2
声明3

完全一样 我以为
声明1
{
声明2;
}
声明3

整个代码:

观察员h

#ifndef Observer_Pattern_Framework__
#define Observer_Pattern_Framework__

// ==== Observer Pattern Framework ============================================

#include <list>
#include <algorithm>

using namespace std;

namespace FIXUP {
//  FIXUP Needed when using BC5.02 because mem_fun is not implemented.
//  FIXUP Needed when using VC5.0 because mem_fun implementatiom in
//  function.h has problems with member functions that return void.
    template<class T>
    class mem_fun_t: public unary_function<T*, void> {
        void(T::*pmf)();
    public:
        explicit mem_fun_t(void(T::*p)()): pmf(p) {
        }
        void operator()(T* p) const {
            (p->*pmf)();
        }
    };

    template<class T> mem_fun_t<T> mem_fun(void(T::*f)()) {
        return mem_fun_t<T>(f);
    }
}
// END FIXUP

// == Observer ==

class SubjectIsDestroyed {}; // Exception

class Subject;

class Observer {
public:
    virtual void update()=0;
    virtual void destruct();
protected:
    Observer(Subject& s);
    virtual ~Observer();
    const Subject* getPointerToSubject() const  {
        if (pointerToSubject==0)
        throw SubjectIsDestroyed();
        return pointerToSubject;
    }
private:
    Subject* pointerToSubject;
    Observer(const Observer&);
    void operator=(const Observer&);
};

// == Subject ==

class Subject {
public:
    void attach(Observer* o) {
        observers.push_front(o);
    }
    void detach(Observer* o) {
        observers.remove(o);
    }
protected:
    Subject() {
    }
    virtual ~Subject() {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(), mem_fun(&Observer::destruct));
    }
    void notify() const {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(), mem_fun(&Observer::update));
    }
private:
    list<Observer*> observers;
    Subject(const Subject&);
    void operator=(const Subject&);
};

inline Observer::Observer(Subject& s): pointerToSubject(&s) {
    pointerToSubject->attach(this);
}
inline Observer::~Observer() {
    if (pointerToSubject)
        pointerToSubject->detach(this);
}
inline void Observer::destruct() {
    pointerToSubject->detach(this);
    pointerToSubject=0;
}

// == Model<T> ==

template <class T>
class Model: public Subject {
public:
    Model(): value(0) {
    }
    Model(const T& init): value(init) {
    }
    virtual void set(const T& v) {
        value=v;
        notify();
    }
    virtual const T& get() const {
        return value;
    }
protected:
    T value;
};

// == CallOnwrite<T, C> ==

template <class T, class C>
class CallOnWrite: public Observer {
public:
    typedef void (C::*PF)();
    CallOnWrite(Model<T>& m, C* po, PF pfu):
        Observer(m),
        pointerToComponent(po),
        pointerToUpdateFunction(pfu) {
    }
    const Model<T>& operator*() const {
        return static_cast<const Model<T>&>(*getPointerToSubject());
    }
    const Model<T>* operator->() const {
        return static_cast<const Model<T>*>(getPointerToSubject());
    }
protected:
    virtual void update() {
        (pointerToComponent->*pointerToUpdateFunction)();
    }
private:
    PF pointerToUpdateFunction;
    C* pointerToComponent;
};

#endif
#ifndef观察者(模式)框架__
#定义观察者模式框架__
//==观察者模式框架============================================
#包括
#包括
使用名称空间std;
名称空间修正{
//使用BC5.02时需要修复,因为没有实现mem_fun。
//使用VC5.0时需要修复,因为在
//h函数与返回void的成员函数有问题。
模板
类mem\u fun\t:公共一元函数{
无效(T::*pmf)();
公众:
显式mem_fun_t(void(t::*p)():pmf(p){
}
void运算符()(T*p)常量{
(p->*pmf)();
}
};
模板mem_-fun_-t mem_-fun(void(t::*f)(){
返回mem_fun_t(f);
}
}
//结束固定
//==观察员==
类subjectsDestroyed{};//例外情况
班级科目;
类观察员{
公众:
虚拟void update()=0;
虚空析构函数();
受保护的:
观察员(学科与科学);
虚拟观察者();
常量主题*GetPointerToObject()常量{
如果(指针对象==0)
抛出主语选词();
返回指针对象;
}
私人:
主题*指向对象的指针;
观察者(常数观察者&);
void运算符=(const Observer&);
};
//==主题==
班级科目{
公众:
无效附加(观察员*o){
观察者:向前推(o);
}
无效分离(观察员*o){
观察员:删除(o);
}
受保护的:
主题(){
}
虚拟主题(){
使用FIXUP::mem_-fun;
对于每个(Observer.begin()、Observer.end()、mem_-fun(&Observer::destruct));
}
void notify()常量{
使用FIXUP::mem_-fun;
对于每个(Observer.begin()、Observer.end()、mem_fun(&Observer::update));
}
私人:
观察员名单;
主语(const主语&);
void运算符=(const Subject&);
};
内联观察者::观察者(主题和对象):指针对象和对象{
指针对象->附加(此);
}
内联观察者::~Observer(){
if(指针对象)
指针对象->分离(此);
}
内联void Observer::destruct(){
指针对象->分离(此);
指针对象=0;
}
//==模型==
模板
班级模式:公共学科{
公众:
模型():值(0){
}
模型(常数T和初始值):值(初始值){
}
虚空集(常数T&v){
值=v;
通知();
}
虚拟常量T&get()常量{
返回值;
}
受保护的:
T值;
};
//==CallOnwrite==
模板
类CallOnWrite:公共观察者{
公众:
typedef void(C::*PF)();
CallOnWrite(型号与维护、C*po、PF pfu):
观察员(m),
指针组件(po),
指针输出日期函数(pfu){
}
常量模型和运算符*()常量{
返回静态_cast(*getPointerToObject());
}
常量模型*运算符->()常量{
返回static_cast(getPointerToSubject());
}
受保护的:
虚拟空间更新(){
(pointerToComponent->*pointerToUpdateFunction)();
}
私人:
PF pointertoupdate函数;
C*指针组件;
};
#恩迪夫
Test.cpp

#include <iostream>
#include <iomanip>
#include <string>

// ==== Observer Pattern Framework ============================================

#include <list>
#include <algorithm>

using namespace std;

namespace FIXUP {
//  FIXUP Needed when using BC5.02 because mem_fun is not implemented.
//  FIXUP Needed when using VC5.0 because mem_fun implementatiom in
//  function.h has problems with member functions that return void.
template<class T>
class mem_fun_t: public unary_function<T*, void> {
    void (T::*pmf)();
public:
    explicit mem_fun_t(void (T::*p)()) :
            pmf(p) {
    }
    void operator()(T* p) const {
        (p->*pmf)();
    }
};

template<class T> mem_fun_t<T> mem_fun(void (T::*f)()) {
    return mem_fun_t<T>(f);
}
}
// END FIXUP

// == Observer ==

class SubjectIsDestroyed {
};
// Exception

class Subject;

class Observer {
public:
    virtual void update()=0;
    virtual void destruct();
protected:
    Observer(Subject& s);
    virtual ~Observer();
    const Subject* getPointerToSubject() const {
        if (pointerToSubject == 0)
            throw SubjectIsDestroyed();
        return pointerToSubject;
    }
private:
    Subject* pointerToSubject;
    Observer(const Observer&);
    void operator=(const Observer&);
};

// == Subject ==

class Subject {
public:
    void attach(Observer* o) {
        observers.push_front(o);
    }
    void detach(Observer* o) {
        observers.remove(o);
    }
protected:
    Subject() {
    }
    virtual ~Subject() {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(),
                mem_fun(&Observer::destruct));
    }
    void notify() const {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(),
                mem_fun(&Observer::update));
    }
private:
    list<Observer*> observers;
    Subject(const Subject&);
    void operator=(const Subject&);
};

inline Observer::Observer(Subject& s) :
        pointerToSubject(&s) {
    pointerToSubject->attach(this);
}
inline Observer::~Observer() {
    if (pointerToSubject)
        pointerToSubject->detach(this);
}
inline void Observer::destruct() {
    pointerToSubject->detach(this);
    pointerToSubject = 0;
}

// == Model<T> ==

template<class T>
class Model: public Subject {
public:
    Model() :
            value(0) {
    }
    Model(const T& init) :
            value(init) {
    }
    virtual void set(const T& v) {
        value = v;
        notify();
    }
    virtual const T& get() const {
        return value;
    }
protected:
    T value;
};

// == CallOnwrite<T, C> ==

template<class T, class C>
class CallOnWrite: public Observer {
public:
    typedef void (C::*PF)();
    CallOnWrite(Model<T>& m, C* po, PF pfu) :
            Observer(m), pointerToComponent(po), pointerToUpdateFunction(pfu) {
    }
    const Model<T>& operator*() const {
        return static_cast<const Model<T>&>(*getPointerToSubject());
    }
    const Model<T>* operator->() const {
        return static_cast<const Model<T>*>(getPointerToSubject());
    }
protected:
    virtual void update() {
        (pointerToComponent->*pointerToUpdateFunction)();
    }
private:
    PF pointerToUpdateFunction;
    C* pointerToComponent;
};

// ==== Application that uses the Classic Observer Pattern ====================

// == ConcreteSubjects ==

typedef Model<bool> Pin;
typedef Model<short> Register;

// == ConcreteObservers ==


template<class T>
class DecDisplay {
public:
    DecDisplay(Model<T>& m, const string& n) :
            cow(m, this, &DecDisplay::update), name(n) {
    }
private:
    void update() {
        cout << name << " = " << dec << cow->get() << endl;
    }
    CallOnWrite<T, DecDisplay<T> > cow;
    string name;
};



typedef DecDisplay<bool> PinDisplay;

// == Simulator ==

class Simulator {
public:
    Register pc; // program counter
    Pin pin1;
    void run() {
        for (short i(9); i < 16; ++i) {
            if (i % 5 == 0)
                pin1.set(!pin1.get());
            pc.set(i);
            cout  << "for = " << i << endl;
        }
    }
};

// == User Interface ==

int main() {
    Simulator s;

    {
        PinDisplay d3(s.pin1, "Pin 1");
        s.run();
    }

    s.run();

    return 0;
}
#包括
#包括
#包括
//==观察者模式框架============================================
#包括
#包括
使用名称空间std;
名称空间修正{
//使用BC5.02时需要修复,因为没有实现mem_fun。
//使用VC5.0时需要修复,因为在
//h函数与返回void的成员函数有问题。
模板
类mem\u fun\t:公共一元函数{
无效(T::*pmf)();
公众:
显式内存(void(t::*p)():
pmf(p){
}
void运算符()(T*p)常量{
(p->*pmf)();
}
};
模板mem_-fun_-t mem_-fun(void(t::*f)(){
返回mem_fun_t(f);
}
}
//结束固定
//==观察员==
阶级主体{
};
//例外情况
班级科目;
类观察员{
公众:
虚拟void update()=0;
虚空析构函数();
受保护的:
观察员(学科与科学);
虚拟观察者();
常量主题*GetPointerToObject()常量{
如果(指针对象==0)
抛出主语选词();
返回指针对象;
}
私人:
主题*指向对象的指针;
观察者(常数观察者&);
void运算符=(const Observer&);
};
//==主题==
班级科目{
公众:
无效附加(观察员*o){
观察者:向前推(o);
}
无效分离(观察员*o){
观察员:删除(o);
}
受保护的:
主题(){
}
虚拟主题(){
使用FIXUP::mem_-fun;
对于每个(observer.begin(),observer.end(),
mem_-fun(&Observer::destruct));
}
void notify()常量{
使用FIXUP::mem_-fun;
对于每个(observer.begin(),observer.end(),
mem_fun(&Observer::update));
}
私人:
观察员名单;
主语(const主语&);
void运算符=(const Subject&);
};
内联观察者::观察者(受试者(&s):
指针对象(&s){
指针对象->附加(此);
}
内联观察者::~Observer(){
if(指针对象)
指针对象->分离(此);
}
内联void Observer::destruct(){
指针对象->分离(此);
指针对象=0;
}
//==模型==
模板
班级模式:公共学科{
公众:
模型():
值(0){
}
模型(常数T和初始值):
值(初始值){
}
虚空集(常数T&v){
值=v;
通知();
}
虚拟常量T&get()常量{
返回值;
}
受保护的:
T值;
};
//==CallOnwrite==
模板
班
#include <iostream>
#include <iomanip>
#include <string>

// ==== Observer Pattern Framework ============================================

#include <list>
#include <algorithm>

using namespace std;

namespace FIXUP {
//  FIXUP Needed when using BC5.02 because mem_fun is not implemented.
//  FIXUP Needed when using VC5.0 because mem_fun implementatiom in
//  function.h has problems with member functions that return void.
template<class T>
class mem_fun_t: public unary_function<T*, void> {
    void (T::*pmf)();
public:
    explicit mem_fun_t(void (T::*p)()) :
            pmf(p) {
    }
    void operator()(T* p) const {
        (p->*pmf)();
    }
};

template<class T> mem_fun_t<T> mem_fun(void (T::*f)()) {
    return mem_fun_t<T>(f);
}
}
// END FIXUP

// == Observer ==

class SubjectIsDestroyed {
};
// Exception

class Subject;

class Observer {
public:
    virtual void update()=0;
    virtual void destruct();
protected:
    Observer(Subject& s);
    virtual ~Observer();
    const Subject* getPointerToSubject() const {
        if (pointerToSubject == 0)
            throw SubjectIsDestroyed();
        return pointerToSubject;
    }
private:
    Subject* pointerToSubject;
    Observer(const Observer&);
    void operator=(const Observer&);
};

// == Subject ==

class Subject {
public:
    void attach(Observer* o) {
        observers.push_front(o);
    }
    void detach(Observer* o) {
        observers.remove(o);
    }
protected:
    Subject() {
    }
    virtual ~Subject() {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(),
                mem_fun(&Observer::destruct));
    }
    void notify() const {
        using FIXUP::mem_fun;
        for_each(observers.begin(), observers.end(),
                mem_fun(&Observer::update));
    }
private:
    list<Observer*> observers;
    Subject(const Subject&);
    void operator=(const Subject&);
};

inline Observer::Observer(Subject& s) :
        pointerToSubject(&s) {
    pointerToSubject->attach(this);
}
inline Observer::~Observer() {
    if (pointerToSubject)
        pointerToSubject->detach(this);
}
inline void Observer::destruct() {
    pointerToSubject->detach(this);
    pointerToSubject = 0;
}

// == Model<T> ==

template<class T>
class Model: public Subject {
public:
    Model() :
            value(0) {
    }
    Model(const T& init) :
            value(init) {
    }
    virtual void set(const T& v) {
        value = v;
        notify();
    }
    virtual const T& get() const {
        return value;
    }
protected:
    T value;
};

// == CallOnwrite<T, C> ==

template<class T, class C>
class CallOnWrite: public Observer {
public:
    typedef void (C::*PF)();
    CallOnWrite(Model<T>& m, C* po, PF pfu) :
            Observer(m), pointerToComponent(po), pointerToUpdateFunction(pfu) {
    }
    const Model<T>& operator*() const {
        return static_cast<const Model<T>&>(*getPointerToSubject());
    }
    const Model<T>* operator->() const {
        return static_cast<const Model<T>*>(getPointerToSubject());
    }
protected:
    virtual void update() {
        (pointerToComponent->*pointerToUpdateFunction)();
    }
private:
    PF pointerToUpdateFunction;
    C* pointerToComponent;
};

// ==== Application that uses the Classic Observer Pattern ====================

// == ConcreteSubjects ==

typedef Model<bool> Pin;
typedef Model<short> Register;

// == ConcreteObservers ==


template<class T>
class DecDisplay {
public:
    DecDisplay(Model<T>& m, const string& n) :
            cow(m, this, &DecDisplay::update), name(n) {
    }
private:
    void update() {
        cout << name << " = " << dec << cow->get() << endl;
    }
    CallOnWrite<T, DecDisplay<T> > cow;
    string name;
};



typedef DecDisplay<bool> PinDisplay;

// == Simulator ==

class Simulator {
public:
    Register pc; // program counter
    Pin pin1;
    void run() {
        for (short i(9); i < 16; ++i) {
            if (i % 5 == 0)
                pin1.set(!pin1.get());
            pc.set(i);
            cout  << "for = " << i << endl;
        }
    }
};

// == User Interface ==

int main() {
    Simulator s;

    {
        PinDisplay d3(s.pin1, "Pin 1");
        s.run();
    }

    s.run();

    return 0;
}