ProgrammingNeed help with C++ template functionsPosted:

Frostd
  • Rising Star
Status: Offline
Joined: Jul 15, 20164 Year Member
Posts: 792
Reputation Power: 118
Trying to do an assignement and was wondering if anyone knew how to convert a simple class to a template class?

Code:
class TwoValues
{
public:
TwoValues(int i, int j);
bool equal();
private:
int a, b;
};
TwoValues::TwoValues(int i, int j)
: a(i), b(j)
{
}
bool TwoValues::equal()
{
return a == b;
}
#2. Posted:
CriticaI
  • Summer 2018
Status: Offline
Joined: Nov 05, 20137 Year Member
Posts: 2,691
Reputation Power: 155
I don't what you're trying to do but start here. geeksforgeeks.org/templates-cpp/


template <typename T>
T myUniversalComparison(T variable1, T variable2)
{
   return x > y;
}


use it like this and don't use my terrible function and variable names.

bool is1GT2 = myUniversalComparison<int>(1,2);
bool is12GT13 = myUniversalComparison<double>(1.2, 1.3);
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS