Invoking a method of a class with template
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Invoking a method of a class with template

  1. #1
    Join Date
    Dec 2010
    Posts
    601

    Invoking a method of a class with template

    Code:
    bool CObject<class T>::Create(LPDIRECT3DDEVICE9 pDevice, T *pMesh, float fAnimSpeed)
    {
    	if(pMesh == NULL)
    		return false;
    
    	m_pDevice = pDevice;
    	m_pMesh = pMesh;	
    	
    	if(m_pMesh->GetAnimController() != NULL)
    	{
    		DWORD dwNumAnimSet = m_pMesh->GetAnimController()->GetMaxNumAnimationSets();
    In here, T can be from the class CMesh * or CSkinnedMesh *, both have signature of GetAnimController
    This maybe a flaw in my design... But I want Create to accept any kinds of meshes.
    Should I create an interface or abstract class above all of those?

    Update:
    I have added an IMesh class that every derived classes has to have the needed interface.


    But since then, I have another question, I feel that it is very cumbersome to include a template argument in every class/prototype that is associated with CObject
    Say
    bool CreatePlanesFromObject(CObject *pObject, D3DXPLANE *pPlanes);
    Requires me to do this
    template<class T> bool CreatePlanesFromObject(CObject<T>* pObject, D3DXPLANE* pPlanes);
    Any ways I can omit the template argument, because it doesn't make sense to know the Mesh type in
    other classes....
    Thanks
    Jack
    Last edited by lucky6969b; August 1st, 2014 at 01:58 AM.

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,886

    Re: Invoking a method of a class with template

    looks like you have a somewhat awkward design. But without seeing the whole class, it's possible this is how it has to be.

    however

    consider.
    CObject just being as it is, not as a template class
    Create the function as a template FUNCTION rather than templatizing the whole class.
    you can then specialize the function for the different types of meshes. (and provide a default one if possible).

    this means you could skip potentially expensive tests like that test for GetAnimController for a mesh type that will never have one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center