Last active
December 17, 2015 17:29
-
-
Save Zetaeta/5646731 to your computer and use it in GitHub Desktop.
Template class example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
class Array5 { | |
public: | |
T data[5]; | |
}; | |
int main() { | |
Array5<int> intarr; | |
intarr.data[0] = 3; | |
Array5<string> strarr; | |
strarr.data[2] = "meow"; | |
} | |
//When the compiler compiles it, it sees that you use the class Array5<int> and Array5<string>. It generates these classes by replacing every instance of "T" in Array5 with "int" or "string", and the result would look like: | |
class Array5<int> { | |
public: | |
int data[5]; | |
}; | |
class Array5<string> { | |
public: | |
string data[5]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment