ProgrammingNeed help with C++ template functions
Posted:

ProgrammingNeed help with C++ template functionsPosted:

Frostd
  • Rising Star
Status: Offline
Joined: Jul 15, 20167Year Member
Posts: 795
Reputation Power: 118
Status: Offline
Joined: Jul 15, 20167Year 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
  • TTG Addict
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
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