ProgrammingNeed help with C++ template functions
Posted:

ProgrammingNeed help with C++ template functionsPosted:

Frostd
  • WIP 5.1
Status: Offline
Joined: Jul 15, 20168Year Member
Posts: 795
Reputation Power: 118
Status: Offline
Joined: Jul 15, 20168Year Member
Posts: 795
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
  • Shoutbox Hero
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,749
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,749
Reputation Power: 452
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