I know the point of templates is to generalize your code, however I would like one specific member function of that class to react differently based on what type of object was created.
Specifically I created a Class Dictionary that is meant to be used to create DictionaryNoun or DictionaryAdjective objects. I have a Dictionary::print() that I want to have a code structure as follows:
Dictionary::print(){
if(this is a Dictionary<Noun> object){
// Print my nouns in some special way
}
if(this is a Dictionary<Adjective> object){
// Print my adjectives in some special way
}
else{ //Print objects in default way}
}
My question is how do I do the type check on my objects?
C++ lets you specialize member functions for specific template arguments. For example, if you have something like this:
Then you can specialize what
printdoes for aDictionary<Noun>by writingYou can specialize for
Adjectivethe same way. Finally, you can write a default implementation that’s used if neither matches by writingHope this helps!