Skip to content

Instantly share code, notes, and snippets.

@jitpaul

jitpaul/Intro1 Secret

Created May 4, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitpaul/ba418bee758558d8b3abd14645aa6192 to your computer and use it in GitHub Desktop.
Save jitpaul/ba418bee758558d8b3abd14645aa6192 to your computer and use it in GitHub Desktop.
Templates
#include <iostream>
using namespace std;
/* Template Function. 'template <class T>' or 'template <typename T>'
can be used here.*/
template <class T>
void exampleFunction(T val) {
cout << val;
}
/* Template Class. 'template <class T>' or 'template <typename T>'
can be used here.*/
template <class T>
class ExampleClass {
T memberVariable;
public:
ExampleClass(T val) :memberVariable(val) {}
void printMemberVaraible() {
cout << memberVariable;
}
};
int main() {
exampleFunction(5); //correct
exampleFunction<int>(5); //correct. 'int' type passed as template argument is optional;
ExampleClass ec1(4); //incorrect. 'int' Type needs to passed as a template argument.
ExampleClass<int> ec1(4);//correct
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment