Help with template object

Discussion in 'C++' started by mickey, Jan 24, 2006.

  1. mickey

    mickey Guest

    I have the following template class which I use as a c++ generic callback.

    template< class T >
    class CallBack
    {
    public:
    typedef void ( T::*Method )( void );

    CallBack( void ) : m_classInstance( NULL ), m_method( NULL ){}

    CallBack( T* classInstance, Method method ) : m_classInstance(
    classInstance ), m_method( method ){}

    void operator()( void ) const
    {
    if( m_classInstance != NULL && m_method != NULL )
    ( m_classInstance->*m_method )();
    };

    private:
    T* m_classInstance;
    Method m_method;
    };

    I have another class which has a method that takes a template class as a
    param...

    class foo
    {
    public:
    foo( void );

    template< class T >
    const boolean Load( CallBack< T > const& callBack, const int16
    imgResID )
    {
    return TRUE;
    }

    CallBack<class T> m_callback; // member I would like to assign the
    callback refernce to
    }

    I would like class foo to have a member to which I can assign the
    callback reference passed in the Load method.

    My problem is that the compiler complains about the difference in types
    between m_callback and the callback param. I am not sure how to declare
    the m_callback.

    Thanks.
     
    mickey, Jan 24, 2006
    #1
    1. Advertising

  2. mickey

    mickey Guest

    red floyd wrote:
    > mickey wrote:
    >
    >>
    >>
    >> I have the following template class which I use as a c++ generic
    >> callback.
    >>
    >> template< class T >
    >> class CallBack
    >> {
    >> public:
    >> typedef void ( T::*Method )( void );
    >>
    >> CallBack( void ) : m_classInstance( NULL ), m_method( NULL ){}
    >>
    >> CallBack( T* classInstance, Method method ) : m_classInstance(
    >> classInstance ), m_method( method ){}
    >>
    >> void operator()( void ) const
    >> {
    >> if( m_classInstance != NULL && m_method != NULL )
    >> ( m_classInstance->*m_method )();
    >> };
    >>
    >> private:
    >> T* m_classInstance;
    >> Method m_method;
    >> };
    >>
    >> I have another class which has a method that takes a template class as
    >> a param...
    >>
    >> class foo
    >> {
    >> public:
    >> foo( void );
    >>
    >> template< class T >
    >> const boolean Load( CallBack< T > const& callBack, const int16
    >> imgResID )
    >> {
    >> return TRUE;
    >> }
    >>
    >> CallBack<class T> m_callback; // member I would like to assign the
    >> callback refernce to
    >> }
    >>
    >> I would like class foo to have a member to which I can assign the
    >> callback reference passed in the Load method.
    >>
    >> My problem is that the compiler complains about the difference in
    >> types between m_callback and the callback param. I am not sure how to
    >> declare the m_callback.
    >>

    >
    > I believe you have to make foo a template as well.
    >
    > i.e.:
    >
    > template <typename T>
    > class foo
    > {
    > public:
    > foo( void );
    >
    > const boolean Load( CallBack< T > const& callBack,
    > const int16 imgResID )
    > {
    > return TRUE;
    > }
    >
    > CallBack<T> m_callback; // member I would like to assign the
    > };
    >
    > Also, should m_callback be a reference?
    >


    Red,

    Thanks for your reply. m_callback needs to be a copy, not a reference,
    as the passed in reference will be destroyed after the call to Load.

    The foo class is specific and making it into a template would create a
    slew of compiling problems for my existing project.
     
    mickey, Jan 24, 2006
    #2
    1. Advertising

  3. mickey

    red floyd Guest

    mickey wrote:
    >
    >
    > I have the following template class which I use as a c++ generic callback.
    >
    > template< class T >
    > class CallBack
    > {
    > public:
    > typedef void ( T::*Method )( void );
    >
    > CallBack( void ) : m_classInstance( NULL ), m_method( NULL ){}
    >
    > CallBack( T* classInstance, Method method ) : m_classInstance(
    > classInstance ), m_method( method ){}
    >
    > void operator()( void ) const
    > {
    > if( m_classInstance != NULL && m_method != NULL )
    > ( m_classInstance->*m_method )();
    > };
    >
    > private:
    > T* m_classInstance;
    > Method m_method;
    > };
    >
    > I have another class which has a method that takes a template class as a
    > param...
    >
    > class foo
    > {
    > public:
    > foo( void );
    >
    > template< class T >
    > const boolean Load( CallBack< T > const& callBack, const int16
    > imgResID )
    > {
    > return TRUE;
    > }
    >
    > CallBack<class T> m_callback; // member I would like to assign the
    > callback refernce to
    > }
    >
    > I would like class foo to have a member to which I can assign the
    > callback reference passed in the Load method.
    >
    > My problem is that the compiler complains about the difference in types
    > between m_callback and the callback param. I am not sure how to declare
    > the m_callback.
    >


    I believe you have to make foo a template as well.

    i.e.:

    template <typename T>
    class foo
    {
    public:
    foo( void );

    const boolean Load( CallBack< T > const& callBack,
    const int16 imgResID )
    {
    return TRUE;
    }

    CallBack<T> m_callback; // member I would like to assign the
    };

    Also, should m_callback be a reference?
     
    red floyd, Jan 24, 2006
    #3
  4. mickey

    TB Guest

    mickey sade:
    > red floyd wrote:
    >> mickey wrote:
    >>
    >>>
    >>>
    >>> I have the following template class which I use as a c++ generic
    >>> callback.
    >>>

    <code snipped>
    >>>
    >>> I would like class foo to have a member to which I can assign the
    >>> callback reference passed in the Load method.
    >>>
    >>> My problem is that the compiler complains about the difference in
    >>> types between m_callback and the callback param. I am not sure how to
    >>> declare the m_callback.
    >>>

    >>
    >> I believe you have to make foo a template as well.
    >>
    >> i.e.:
    >>
    >> template <typename T>
    >> class foo
    >> {
    >> public:
    >> foo( void );
    >>
    >> const boolean Load( CallBack< T > const& callBack,
    >> const int16 imgResID )
    >> {
    >> return TRUE;
    >> }
    >>
    >> CallBack<T> m_callback; // member I would like to assign the
    >> };
    >>
    >> Also, should m_callback be a reference?
    >>

    >
    > Red,
    >
    > Thanks for your reply. m_callback needs to be a copy, not a reference,
    > as the passed in reference will be destroyed after the call to Load.
    >
    > The foo class is specific and making it into a template would create a
    > slew of compiling problems for my existing project.


    Then add another non-template base class which declares operator() virtual:

    class CallBackBase {
    public:
    virtual void operator()() const throw () = 0;
    };


    template<typename T>
    class CallBack : public CallBackBase {
    public:
    void operator()() const throw () {

    }
    };

    class Foo {
    public:
    template<typename T>
    void Load(CallBack<T> const & callBack) throw (std::bad_alloc) {
    // Copying
    m_callback = new CallBack<T>(callBack);
    }
    void Call() {
    (*m_callback)();
    }
    CallBackBase * m_callback;
    };

    --
    TB @ SWEDEN
     
    TB, Jan 24, 2006
    #4
  5. mickey

    mickey Guest

    Yeah that works, thank you TB.
     
    mickey, Jan 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    493
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    556
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,137
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    801
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    549
Loading...

Share This Page