Reputation: 831
I am creating observer template sample in C++ on windows.
Here there is an agent which has a list of customers. Whenever an entity(variable x) of the agent changes it notifies its customers about the same and passes the value of x to customers. The customers then store this value in their respective variables.
In the below code the agent acts as subject and the customers act as observers. The agents are created from their agent template class and the customers are created from their customer template class.
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);
}
I get compilation error
error C2664: 'agent<T>::addCust' : cannot convert parameter 1 from 'customerC' to 'customer<T> *'
I know the way I have called addCust is wrong but still not getting any idea as to call it. Any hint to resolve this issue?
Also is the way I have created agents class correct?
class agentS: public agent<agentS>
When I call addCust() function I pass observer objects.
Upvotes: 0
Views: 178
Reputation: 1366
By creating your agentS class that way, the effective signature for addCust becomes void addCust(customer<agentS>* cobj);
. However, your customer classes are not templated on the agent type (there doesn't actually seem to be a reason for it to be templated).
You appear to be mixing dynamic polymorphism (inheritance and virtual functions with customer) and static polymorphism (templates to create a vector of one type of customer). Either of these options on their own would make more sense:
Dynamic polymorphism (inheritance). You can store different types of customer in the same container, by storing the base class pointer, and use the customer base class and virtual functions to tread them in the same way:
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);
}
Static polymorphism (templates). The agent class is templated on the customer type, so the vector can only contain one type of customer, but it's easy to create a specific agent for any given customer type:
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);
}
Upvotes: 1