简体   繁体   中英

Can a template parameter be the type of an object that will be known at run-time?

Let's say I have a class Grid of 3 different kinds of squares each with different functionalities, all of which inherit from the parent class Square . I want to create a template like this where T1 is whatever the type of the current square (that my character is in) is. It could be any square in the Grid but that's only known when the function is called.

template <typename T1, typename T2> void Grid::move(T1 current_square, T2 new_square){...}

No, the template type has to be resolved at compile-time. You'll want to make them both Square&, but you'll need some way for the object to tell you what kind of Square it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM