开发者

templates in C++ [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why can templates only be implemented in the header file?

hello everyone I've just read that if I write some template I must write all code in header file (including constructor, desctructors...), but I can't understand why, can somebody please e开发者_JAVA百科xplain?


When a template is used, it's much like the defined template class or function is used to create a new class by replacing the template parameters with the provided parameters.

The code for this new class is compiled right when it's needed, so all the source needs to be there at that time.

See this link at the bottom of the page for a bit more detail.


Because the templated code can be compiled only after it is know, which class/type replaces the template, so all code must be available then.

There is a very detailed explanation of the details in the C++ FAQ

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜