-1

There are some structures in C++ which instances can be defined as

my_structure<type> variable;

For example, std::vector

std::vector<int> very_important_vector;

And I would like to know is there any way to make my custom structure, class, or whatever definable as shown above.

Thanks in advance for your answers.

1 Answers1

4

is there any way to make my custom structure, class, or whatever definable as shown above.

Yes, these are called class templates. One example is given below:

template<typename T>
struct my_structure
{
    //members here
};

int main ()
{
  //now you can write 
  my_structure<int> variable;//this will instantiate my_structure<int> 
  
  my_structure<float> variable2; //this will instantiate my_structure<float>
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jason
  • 36,170
  • 5
  • 26
  • 60