开发者

C++ Template Usage

If I have a template definition like the one below, can someone provide a code sample for how I would actually instantiate an instance of this with two of my own classes?

template <class T1, class T2>
class LookUpTable { 
public:
    LookUpTable(); 
    void set(T1 x, T2* y);
    开发者_开发百科T2* get(T1 x);
};

Thanks.


You can't instantiate it unless you provide a definition for the constructor. And you won't be able to use it unless you provide definitions for the other two functions. If you did provide them, you would instantiate it something like:

LookUpTable <std::string, int> t;

or if you have your own classes A and B:

LookUpTable <A, B> t;

It looks like this is a map of some sort, in which case you may as well use std::map:

#include <map>
#include <string>

std::map <std::string, int> m; 


1) Since this is a template class, make sure your constructor and functions are declared in the header.

2) Instantiate it like this:

LookUpTable <YourClass1, YourClass2> table;

3) Note: you have a typo, Tl instead of T1. In some fonts l looks almost like 1.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜