passing template class as temporary object

i have a template classa as shown below, i want to call it in for_each method using the temporary object... how can i do this. i have already done this by class variable, but i have to do same by temporary object.. can anybody help thanks

template <class T> class My_action
{

public:

My_action(T a)
{
cout << a << endl;
}
void operator() (T a)
{
cout << a << endl;
}
};


int main()
{

vector<int> vi(MAX);
vector<double> vd(MAX);
vector<string> vs(MAX);

vi.at(0) = 2;
vi.at(1) = 4;
vi.at(2) = 6;
vi.at(3) = 8;
vi.at(4) = 10;


My_action<int> i_action;
for_each(vi.begin(),vi.end(),i_action );

}
Is this what you mean?
1
2
//My_action<int> i_action;
for_each(vi.begin(),vi.end(),My_action<int>());


If that doesn't work, try:
for_each(vi.begin(),vi.end(),My_action<int>()());
Last edited on
Topic archived. No new replies allowed.