Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ class template

#include <vector>

// This is your own template
// T it's just a type
template <class T1, class T2, typename T3, typename T4 = int>
class MyClass
{
  public:
  	MyClass() { }
  
  private:
  	T1 data; 		// For example this data variable is T type
  	T2 anotherData;	// Actually you can name it as you wish but
  	T3 variable;	// for convenience you should name it T
}

int main(int argc, char **argv)
{
  std::vector<int> array(10);
  //          ^^^
  // This is a template in std library
  
  MyClass<int> object();
  // This is how it works with your class, just a template for type
  // < > angle brackets means "choose" any type you want
  // But it isn't necessary should work, because of some reasons
  // For example you need a type that do not supporting with class
  return (0);
}
Comment

C++ Class Template Declaration

template <class T>
class className {
  private:
    T var;
    ... .. ...
  public:
    T functionName(T arg);
    ... .. ...
};
Comment

C++ Creating a Class Template Object

className<int> classObject;
className<float> classObject;
className<string> classObject;
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ region 
Cpp :: difference between --a and a-- c++ 
Cpp :: swap in cpp 
Cpp :: passing structure to function in c++ example 
Cpp :: system("pause") note working c++ 
Cpp :: double array size c++ 
Cpp :: enum c++ 
Cpp :: how to initialize a queue in c 
Cpp :: c++ shell 
Cpp :: convert std vector to array 
Cpp :: c++ inheritance constructor 
Cpp :: il2cpp stuck unity 
Cpp :: vector from angle 
Cpp :: how to change the value of a key in hashmp in c++ 
Cpp :: reference c++ 
Cpp :: c++ code executio canntot proceed because glew32.dll was not founud 
Cpp :: c++ if statement 
Cpp :: c++ json parser euc-kr 
Cpp :: how to print items in c++ 
Cpp :: Basic Input / Output in C++ 
Cpp :: c++ split string 
Cpp :: c++ classes 
Cpp :: evennumbers 1 to 100 
Cpp :: shift element to end of vector c++ 
Cpp :: c++ else if 
Cpp :: ? in cpp 
Cpp :: min heap 
Cpp :: reverse in vector c++ 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: store arbitrarly large vector of doubles c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =