开发者

c++ generic object [closed]

开发者 https://www.devze.com 2023-03-03 16:05 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

HI开发者_Go百科,

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!


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.


Or you can pass untyped pointer:

void find(std::string, void* object)
{
    //code
}

It depends on what you are going to do inside find().

0

精彩评论

暂无评论...
验证码 换一张
取 消