Generic Classes in C++

Atharwa Kharkar
5 min readMay 12, 2021
Generic C++ Classes

Generics are parameterized types supported by the common language runtime. A parameterized type is a type that is defined with an unknown type parameter that is specified when the generic is used.
A powerful feature of C++ is that you can make ​template classes​ which are ​classes ​that can have ​members ​of the ​generic​ type, i.e., members that use template parameters as types.

Generic programming is a way to write functions and data types while making minimal assumptions about the type of data being used.

Generics is the idea to allow type (Integer, String, … etc. and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc., which can be used using generics very efficiently. We can use them for any type.

The method of Generic Programming is implemented to increase the efficiency of the code. Generic Programming enables the programmer to write a general algorithm which will work with all data types. It eliminates the need to create different algorithms if the data type is an integer, string or a character.

Why Generics?

C++ supports templates and both templates and generics support parameterized types to create typed collection classes. However, templates provide compile-time parameterization. You cannot reference an assembly containing a template definition and create new specializations of the template. Once compiled, a specialized template looks like any other class or method. In contrast, generics are emitted in MSIL as a parameterized type known by the runtime to be a parameterized type; source code that references an assembly containing a generic type can create specializations of the generic type.

C++ implements generic programming concepts through templates. Templates give the compiler a framework to generate code for the types it gets implemented with. With a class, C++ will look at your template as well as the type specified when you created the object, and generate that typed class for you.

Templates are the mechanism by which C++ implements the generic concept. Simply, they allow you to pass data type as a parameter so that you don’t need to write the same code for different data types.

Think of it as a cookie cutter in the shape of a star. You can have various kinds of cookie dough, like shortbread, or chocolate-shortbread or chocolate chip cookie dough. The cookie cutter doesn’t care about the ingredients, as long as it is dough.

When a class uses the concept of Template, then the class is known as generic class.

Generic Class using Template

Class Template are useful when a class defines something that is independent of the data type. Can be useful for classes like Linked List, binary tree, Stack, Queue, Array, etc.

Generic Class Using Templates

Output:

Output Terminal

We can pass more than one data types as arguments to templates.

Passing Multiple Arguments

Output:

Output

Static Variables

On the creation of a new generic type, new instances of any static variables are created and any static constructor for that type is executed.

Static variables can use any type parameters from the enclosing class.

Methods in Generic Classes

Methods in generic classes can be generic themselves; non-generic methods will be implicitly parameterized by the class type parameter.

The following special rules apply to methods within generic classes:

  • Methods in generic classes can use type parameters as parameters, return types, or local variables.
  • Methods in generic classes can use open or closed constructed types as parameters, return types, or local variables.

On the creation of a new generic type, new instances of any static variables are created and any static constructor for that type is executed.

The Advantages of Generic Classes are,

Code Reusability

Avoid Function Overloading

Once written it can be used for multiple times and cases.

Static variables can use any type parameters from the enclosing class.

Ultimately, generics are another tool which you can use to make to code more maintainable because they help distill essential functionality and promote readability. At the end of the day, it doesn’t make a huge amount of difference to the compiler whether you copied and pasted functionality all over your code or whether you use generics. Code is meant to be read by people.

Non-Generic Methods in Generic Classes

Methods in generic classes that have no additional type parameters are usually referred to as non-generic although they are implicitly parameterized by the enclosing generic class.

The signature of a non-generic method can include one or more type parameters of the enclosing class, either directly or in an open constructed type.

References

--

--