C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to use smart pointer in template?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Dec 29, 2006 10:10 am    Post subject: how to use smart pointer in template? Reply with quote



Hello gurus,

I have a template function with three types, I want to use the second
template to specify a class pointer or an auto_ptr (or a smart pointer,
etc). However seems I can not work with the latter, only plain naked
pointers are accepted. Is there any workaround? Thanks and happy new
year!
-Grace

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <memory>

using namespace std;

class A
{
public:
int m_i;
A(int j): m_i(j) {}

void foo(int k) { printf("in A, k=%d\n", k); }
void foo_d(double f) { printf("in A, k=%f\n", f);}
void foo2(int k) { printf("in A, k=%d\n", k); }
};


class B
{
public:
int m_i;
B(int j): m_i(j) {}

void foo(int k) { printf("in B k=%d\n", k); }
void foo2(int k) { printf("in B k=%d\n", k); }
};



// this is a class template usage, first class type,
//2nd a type pointer, 3rd input parameter type.
template <typename T, typename TPTR, typename INTYPE>
void AssignValue(TPTR& ptr, void (T::*method)(INTYPE), INTYPE in)
{
(ptr->*method)(in); //call a method, given the input parameter.
return;
}

typedef class A* A_PTR;
typedef class B* B_PTR;
typedef auto_ptr<A> A_PTR2;

int main()
{
A* a = new A(1);
AssignValue<A, A_PTR, int> (a, &A::foo, 20 );
AssignValue<A, A_PTR, double> (a, &A::foo_d, 20.20 );

B* b = new B(1);
AssignValue<B, B_PTR, int>(b, &B::foo2, -10);


auto_ptr<A> aa(new A(10));
AssignValue<A, A_PTR2, int>(aa, &A::foo, -10000);
/* Error above, can not compile, complaining no match for
`std::auto_ptr<A>& ->* void (A::*&)(int)' operator */


return 0;
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.