c++类调用的一个小问题

先看这两段代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Subject
{
public:
    Subject(){}
    virtual ~Subject(){}
    virtual void attach(MyObserver *observer)=0;
    virtual void detach(MyObserver *observer)=0;
    virtual void stockUp()=0;
    virtual void stockDown()=0;
};
class Stock:public Subject
{
public:
    void attach(MyObserver *observer)
    {
        observers.push_back(observer);
    }
    void detach(MyObserver *observer)
    {
        vector<MyObserver*>::iterator observer_rm = find(observers.begin(), observers.end(), observer);
        observers.erase(observer_rm);
    }
    void stockUp()
    {
         cout<<"股票上涨5%"<<endl;
         vector <MyObserver *>::iterator iObserver = observers.begin();
         while(iObserver != observers.end())
         {
              (*iObserver)->response1();
              iObserver++;
         }
    }
     void stockDown()
    {
         vector <MyObserver *>::iterator iObserver = observers.begin();
         cout<<"股票下降5%"<<endl;
         while(iObserver != observers.end())
         {
              (*iObserver)->response2();
              iObserver++;
         }
    }
private:
    vector<MyObserver*> observers;
};
class MyObserver
{
public:
    virtual void response1() = 0;
    virtual void response2() = 0;
};

class Investor:public MyObserver
{
public:
     void response1(){
        cout<<"股民买股票"<<endl;
    }
    void response2(){
        cout<<"股民大哭一场"<<endl;
    }
};
int main()
{
    Stock subject;
    Investor obs;
    subject.attach(&obs);
    subject.stockUp();
    subject.stockDown();
}
c++代码片段一
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyObserver
{
public:
    virtual void response1() = 0;
    virtual void response2() = 0;
};

class Investor:public MyObserver
{
public:
     void response1(){
        cout<<"股民买股票"<<endl;
    }
    void response2(){
        cout<<"股民大哭一场"<<endl;
    }
};
class Subject
{
public:
    Subject(){}
    virtual ~Subject(){}
    virtual void attach(MyObserver *observer)=0;
    virtual void detach(MyObserver *observer)=0;
    virtual void stockUp()=0;
    virtual void stockDown()=0;
};
class Stock:public Subject
{
public:
    void attach(MyObserver *observer)
    {
        observers.push_back(observer);
    }
    void detach(MyObserver *observer)
    {
        vector<MyObserver*>::iterator observer_rm = find(observers.begin(), observers.end(), observer);
        observers.erase(observer_rm);
    }
    void stockUp()
    {
         cout<<"股票上涨5%"<<endl;
         vector <MyObserver *>::iterator iObserver = observers.begin();
         while(iObserver != observers.end())
         {
              (*iObserver)->response1();
              iObserver++;
         }
    }
     void stockDown()
    {
         vector <MyObserver *>::iterator iObserver = observers.begin();
         cout<<"股票下降5%"<<endl;
         while(iObserver != observers.end())
         {
              (*iObserver)->response2();
              iObserver++;
         }
    }
private:
    vector<MyObserver*> observers;
};
int main()
{
    Stock subject;
    Investor obs;
    subject.attach(&obs);
    subject.stockUp();
    subject.stockDown();
}
c++代码片段二

运行后:片段一运行错误,片段二运行成功

后来发现,在Subject中调用MyObserver时,类MyObserver应放在Subject之前,才能调用成功,也就是说在c++中如果想要调用某个类,应放在前面,或者如果不想管顺序,那就需要提前进行类的声明。

用java用习惯了,用c++有点陌生了。

posted @ 2021-11-17 11:17  睡觉不困  阅读(21)  评论(0编辑  收藏  举报