linuxx linuxx - 1 month ago 13
C++ Question

c++ generic object

HI,

I have the following code:

class Libr
{

public:
Libr();
std::string book;

class Street
{
public:
Street();

}*street
}*libr;


How can I use a generic object in the following method:

void find(std::string, ??generic object)
{//code};


Can someone please give me an example with the generic object applied for both the classes I wrote?THX!

Answer

You can make it a function template:

template<class T>
void find(std::string s, T & object)
{
    //code
};

You can call this with a parameter of any type. Read this to get more on templates.