4 Replies - 1762 Views - Last Post: 22 July 2010 - 04:14 PM Rate Topic: -----

#1 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Using a template function in a parent class

Posted 20 July 2010 - 03:50 PM

Hi all,

I am hoping that I can do the following rather than defining a function for each child class with the only difference being the type.

I have searched around but I am still not getting anywhere.

The idea is as follows:
class PClass
{
  public:
    PClass* next;
    PClass* prev;
    int parVar;
    template<class T> T* GetFirstNode();
}

class CClass : public PClass
{
  public:
    int childVar;
}

int main(int argc, char** argc)
{
  CClass* ptr;
  GenerateLinkedList(ptr); // Send CClass to get data
  ptr->GetFirstNode();
  ptr = ptr->next;
  std::cout<<"The value of the second childVar is "<<ptr->childVar<<endl;
  return 0;
}




Thanks,
enpey

Is This A Good Question/Topic? 0
  • +

Replies To: Using a template function in a parent class

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Using a template function in a parent class

Posted 21 July 2010 - 11:25 AM

A base class will never know anything about its derived classes, so I'm not sure how you intend to get ahold of the necessary type information at compile time; In addition to this, template functions cannot be virtual, so you will be unable to use it polymorphically anyway.


Could you describe in detail exactly what it is that you are trying to do; i.e. what is PClass? is it a linked list node? What is it intended to store? what does its stored data represent? how will that stored data be used? Why do you feel like you need to inherit from PClass?

Bottom line is - I have a feeling that inheriting from a linked list node is probably not the right solution to your problem.

Also, when designing classes in a heirarchy you should consider their interface (The interface describes what those classes will actually do) - if you have any specific need to know the type of a derived class at runtime (Either through RTTI or some other mechanism), then your overall design/approach probably needs rethinking from scratch - behaviour which is specific to any one type should ideally be encapsulated within that type, otherwise you'll end up with ugly if-else statements everywhere.

This post has been edited by Bench: 21 July 2010 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Using a template function in a parent class

Posted 21 July 2010 - 04:51 PM

I am looking at generating a simple 2D mesh. The parent class simply contains the linked list pointers, and functions to improve the usability of the class(es).

I want to create a linked list of coordinates, a linked list of edges, and a linked list of triangles.

Each of these objects would would contain an array (coordinate array of three floating point values, edge array of two pointers to coordinate objects, and a triangle array of three pointers to edge objects). To improve the use of these linked lists, each would benefit from GetFirstNode() and GetEndNode() style functions. If the child classes all inherited the next and prev pointers I thought the parent class would be able to implement the function as it does not matter which child class is being used, however the return type is wrong.

Would the functions GetFirstNode(), GetLastNode(), AppendNode(NodeType* ptr), AddNodeBefore(NodeType* ptr) etc. be better implemented outside the class (but as a function template within the class implementations)?


Thanks,
enpey
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Using a template function in a parent class

Posted 22 July 2010 - 12:19 PM

With regards to the code that you've posted, GetFirstNode probably doesn't need to be a template function - it should be enough to return PClass*


However, if you're going to create separate linked lists anyway, I think that you're after an entire template class rather than playing around with derived nodes (Similar to the STL linked list... which brings me to another question - have you considered just using the STL linked list which does all of this for you?)

Using the STL, you might do something like
#include <list>

//TODO: Create the classes
class triangle      { /* etc. */};
class coordinate    { /* etc. */};
class edge          { /* etc. */};

int main()
{
    using namespace std;

    list<triangle> triangles;
    list<coordinate> coordinates;
    list<edge> edges;

    /* etc... */
} 
Typically, you would use iterators to traverse the lists (Preferably with the standard algorithms); to get the first node of triangles, you could use *triangles.begin(), or, more simply, triangles.front(). iterators are essentially 'safe' pointers for use with containers like the STL list.



If the excercise is to write your own linked list, then the template class declaration could look something like
template<typename Type>
struct node
{
    node* next;
    node* prev;
    Type data;
    node(Type data, node* next = 0, node* prev = 0)
        : data(data), next(next), prev(prev) {}
};

template<typename Type>
class linked_list
{
    node<Type>* head;
    node<Type>* tail;
public:
    linked_list() : head(0), tail(0) {}
    Type& front()
    {
        if (head)
            return head->data;
        else
            throw std::runtime_error("List is empty");
    }
}; 
int main()
{
    linked_list<triangle> triangles;
    linked_list<edge> edges;
    linked_list<coordinate> coordinates;
} 


Since your triangle, edge, and coordinate classes seem to be distinctly different, there's probably not much benefit in having 'derived' linked list nodes for each type and handling the polymorphism at runtime (You'd need to invent your own interface which tied the 3 types together, and that would be unwieldy). template classes allow so-called "static polymorphism", saving you retyping the code, but letting you keep hold of the type information for the objects which the list is storing (The biggest disadvantage of "OOP" is that you lose this type information).
Was This Post Helpful? 1
  • +
  • -

#5 enpey  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Using a template function in a parent class

Posted 22 July 2010 - 04:14 PM

Thanks Bench, that was very helpful.

Quote

With regards to the code that you've posted, GetFirstNode probably doesn't need to be a template function - it should be enough to return PClass*

It (gcc) did not like a PClass pointer being returned to a CClass pointer, so I typecast it. This just led to a compile error when I tried to access an element of the child object from the pointer.

Thanks again,
enpey
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1