Monday, May 24, 2010

How to use variant tyeps in c++ ?

hi there


how can i use variant types in c++ as a function argument?





for example foo(variant x){


....





}





int a;


foo (a);


float b;





foo(b)

How to use variant tyeps in c++ ?
A template function will do what you want. Here's a simple example:





#include %26lt;iostream%26gt;





using namespace std;





template %26lt;typename T%26gt;


void foo(T x) {


cout %26lt;%26lt; x %26lt;%26lt; endl;


}





int main(int argc, char *argv[]) {


int i = 1;


float f = 2.2;





foo%26lt;int%26gt;(i);


foo%26lt;float%26gt;(f);


return 0;


}


No comments:

Post a Comment