S
slavinger
Hello,
I was attempting do the following (in VC++):
#define AGE 1
#define NAME 2
template<class T> T getInfo(int what)
{
int age = 20;
string name = "whatever";
if (what == AGE)
return age;
else if (what == NAME)
return name;
}
The idea is that main() can get both kinds of info by calling a single
function, for instance "cout << getInfo(NAME);" I was looking to
implement this function inside a class that has many different types of
private data. The compiler is complaining that it can't deduce the
data type of T. I can see why it's complaining, so I'm wondering: is
there a way to do something like this in C++? Thanks.
I was attempting do the following (in VC++):
#define AGE 1
#define NAME 2
template<class T> T getInfo(int what)
{
int age = 20;
string name = "whatever";
if (what == AGE)
return age;
else if (what == NAME)
return name;
}
The idea is that main() can get both kinds of info by calling a single
function, for instance "cout << getInfo(NAME);" I was looking to
implement this function inside a class that has many different types of
private data. The compiler is complaining that it can't deduce the
data type of T. I can see why it's complaining, so I'm wondering: is
there a way to do something like this in C++? Thanks.