CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    When to use pointer template parameters?

    I created a templated class which searches for a path between 2 points in a graph. The template parameter takes in a class representing the graph.

    template <class graph_type>
    class Graph_PathSearch
    {
    public:
    Graph_PathSearch(&graph_type G, int startIndex, int endIndex);
    };


    The graph object is declared as a pointer.

    NavGraph* m_pGraph;
    m_pGrapn = new NavGraph(true);


    I then instantiated the path search using this statement:

    Graph_PathSearch<NavGraph*> StartSearch(*m_pGraph,startNode,destNode);


    But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.

    I would like to know when is the right time to use a pointer as a template parameter, since the graph is declared as a pointer but using a pointer as a template parameter is incorrect in this case?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: When to use pointer template parameters?

    Quote Originally Posted by ZhiYi View Post
    I then instantiated the path search using this statement:

    Graph_PathSearch<NavGraph*> StartSearch(*m_pGraph,startNode,destNode);


    But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.
    Maybe the errors were legitimate. It's impossible to know if you don't post your entire code.

    Second, if the pointer is typedef'ed, it may resolve some of the errors:
    Code:
    typedef  NavGraph* NavGraphPtr;
    And then use NavGraphPtr.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: When to use pointer template parameters?

    But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.
    Based on the code you posted, you call the function with NavGraph* as the template parameter, but in first argument to that function you dereference the pointer m_pGraph, hence you're actually passing a NavGraph object to the function.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured