As we use typename or class while defining the template.So, what is difference between them and when to write typename and when to write class?
What is the difference between typename & class?
@subham221 we use template before class when data members of class can be of different types.
Lets say you make a class to hold marks value of students, say you define marks variable as int, but now you want to store decimal points as well, in this case you don’t have to define another class(which is same to previous one except variable type!)
If you are sure with variable types then you don’t need to write template before class!
I got your point!. But what am I asking is - we type using two keywords, type name and class in a template. example: template or template. So I am asking is, what is the difference between writing typename and class in a template definition. And specifically, when do we need to write class and when do we need to write typename!??
See tactically class and typename are almost identical except that typename fails when referring nested templates!(example template<template<…> >), otherwise both can be used almost identically. So unless you are using nested templates, you may use anything (or class to be on safer side!)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.