C 样本观察者模板类误差

C++ sample Observer Template Class error

本文关键字:误差 样本 观察者      更新时间:2023-10-16

我正在Windows上的C 创建观察者模板样本。

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

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

template <typename T>
class customer                      // acts as base observer class
{
    char name[50];
public:
    customer()
    {
        cout << __FUNCTION__ "(): " << "DEFAULT CONSn";
    }
    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 consn";
    }
    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 consn";
    }
    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 consn";
    }
    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 << "nnYou 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 << "nnYou 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 consn";
    }
    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()函数时,我通过观察者对象。

通过以这种方式创建代理类,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);
}