C ++示例观察者模板类错误

问题描述 投票:0回答:1

我在Windows上用C ++创建观察者模板示例。

这里有一个代理商,其中有一个客户列表。每当代理的实体(变量x)发生变化时,它就会通知其客户,并将x的值传递给客户。然后,客户将此值存储在各自的变量中。

在下面的代码中,代理充当主体,客户充当观察者。代理程序是从其代理程序模板类创建的,客户是从其客户模板类创建的。

template <typename T>
class customer                      // acts as base observer class
{
    char name[50];

public:
    customer()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT CONS\n";
    }

    customer(char* nm)
    {
        strcpy_s(name, nm);
        cout << __FUNCTION__ "(): " << "name set to " << name << "\n";
    }

    char * getName()
    {
        return(name);
    }

    virtual void update(int c)
    {

    }
};

class customerC: public customer<customerC>
{
    int c;
public:
    customerC()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT customerc cons\n";
    }

    customerC(char* nm):customer<customerC>(nm)
    {
        cout << __FUNCTION__ "(): " << "customer is " << getName() << "\n";
    }

    void update(int val)
    {
        cout << __FUNCTION__ "(): c to " << c << "\n";
        c = val;
    }
};

class customerD: public customer<customerD>
{
    int d;
public:
    customerD()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT customerd cons\n";
    }

    customerD(char* nm):customer<customerD>(nm)
    {
        cout << __FUNCTION__ "(): " << "customer is " << getName() << "\n";
    }

    void update(int val)
    {
        cout << __FUNCTION__ "(): c to " << d << "\n";
        d = val;
    }

};



template<typename T>
class agent
{
    char name[50];
    int x;

protected:
    vector<customer<T>*> custList;

public:
    agent()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT agent cons\n";
    }

    virtual void setx(int c)
    {
        cout << __FUNCTION__ "(): " << "Setting x to " << c << "\n";

////        x = c;
////        notifyObs();
    }

    virtual void getx()
    {
        cout << __FUNCTION__ "(): " << "x = " << x << "\n";
    }

    void addCust(customer<T>* cobj)
    {
        cout << __FUNCTION__ "(): " << "Adding customer " << cobj->getName() << " to list.\n";
        custList.push_back(cobj);
    }

    void showCust()
    {
        cout << __FUNCTION__ "(): " << "Customers are:\n";

        if(custList.empty())
            cout << "\n\nYou have no items.";
        else
        {
            vector<customer<T>*>::iterator cs;
            for(cs = custList.begin(); cs != custList.end(); ++cs)
            {
                cout << (*cs)->getName() << "\n";
            }
        }
    }

    int notifyObs()
    {
        cout << __FUNCTION__ "(): " << "Customers notified are:\n";

        if(custList.empty())
            cout << "\n\nYou have no items.";
        else
        {
            vector<customer<T>*>::iterator cs;
            for(cs = custList.begin(); cs != custList.end(); ++cs)
            {
                cout << (*cs)->getName() << "\n";
                (*cs)->update(x);
            }
        }

        return 0;
    }
};

class agentS: public agent<agentS>
{
    int x;

public:
    agentS()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT agentS cons\n";
    }

    void setx(int c)
    {
        cout << __FUNCTION__ "(): " << "Setting x to " << c << "\n";

        x = c;
        notifyObs();
    }

    void getx()
    {
        cout << __FUNCTION__ "(): " << "x = " << x << "\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    customerC cobj("c1");
    customerD dobj("c2");

    agentS agS;

    agS.addCust(cobj);
////    agS.addCust<customer<customerC>>(cobj);
////    agS.addCust(dobj);
    agS.showCust();
    agS.setx(4);

    return(0);
}

我收到编译错误

error C2664: 'agent<T>::addCust' : cannot convert parameter 1 from 'customerC' to 'customer<T> *'

我知道我调用addCust的方式是错误的,但仍然没有想到要调用它。有什么提示可以解决这个问题吗?

还有我创建代理类正确的方法吗?

class agentS: public agent<agentS>

当我调用addCust()函数时,我传递了观察者对象。

c++ templates observer-pattern
1个回答
1
投票

通过这种方式创建agentS类,addCust的有效签名变为void addCust(customer<agentS>* cobj);。但是,您的客户类不会在代理类型上模板化(实际上似乎没有理由将其模板化)。

您似乎混合了动态多态(继承和虚函数与客户)和静态多态(模板来创建一种类型的客户的向量)。这些选项中的任何一个都会更有意义:

动态多态(继承)。您可以通过存储基类指针在同一容器中存储不同类型的客户,并使用客户基类和虚函数以相同的方式处理它们:

struct customer {};
struct customerC : customer {};
struct customerD : customer {};

struct agent
{
    void addCust(customer* customer) { ... }

    std::vector<customer*> custList;
};

int main()
{
    agent a;
    customerC c;

    a.addCust(&c);
}

静态多态(模板)。代理类是在客户类型上模板化的,因此向量只能包含一种类型的客户,但是为任何给定的客户类型创建特定代理很容易:

struct customer {};
struct customerC : customer {};
struct customerD : customer {};

template<CustomerT>
struct agent
{
    void addCust(CustomerT* customer) { ... }

    std::vector<CustomerT*> custList;
};

int main()
{
    agent<customerC> a;
    customerC c;

    a.addCust(&c);
}
© www.soinside.com 2019 - 2024. All rights reserved.