0

I have written a short template list class defining some methods I would like to use in implementations of it. My current problem is that I am having trouble extending the generic template type and defining a specific type.

In other words, assume I have a class called MyType

List.h:

template<class T>
class List
{
    public:
        Node<T> *head;
        Node<T> *tail;

 ...
}

Inside the Node class:

public:
    virtual T getData();

I want to extend this class with a specific defined type MyType so that I can have getData() behave differently in the extended class MyTypeList. I am having trouble with syntax. Any help would be greatly appreciated!

class MyTypeList : public List<T>
{
...
}
turkycat
  • 35
  • 7
  • Did you want `MyListType` to be a template type as well? – David Brown Dec 02 '12 at 00:39
  • You can have more than one template parameter `template`. Is that what you mean>? – Aesthete Dec 02 '12 at 00:39
  • Not exactly. I want to define the type T to be MyType when using MyTypeList. This way, I could have getData manipulate the MyType object. – turkycat Dec 02 '12 at 00:43
  • First, you can start with defining getData() correctly (it should return a reference, otherwise all your manipulating is a copy of the object managed in the list). Secondly, you're effectively asking to specialize the node-wrapper the list uses to manage the templates-type `T`. *Why* you want to do this (a use-case) is entirely unclear in the delivery of your question, and is likely the reason for the disconnect between what you're asking and what people are hearing. So perhaps a concrete example of usage is in order. – WhozCraig Dec 02 '12 at 15:29

1 Answers1

1

You can do something like this:

struct MyTypeList : List<MyType> {
};

although it is about the same as doing:

typedef List<MyType> MyTypeList;
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • clever, but I'm not sure it will solve my problem. I want to be able to DEFINE the type T = MyType so that when using MyTypeList the user is required to provide objects of type MyType... That way, I could override the getData() method and have it manipulate the MyType object... – turkycat Dec 02 '12 at 00:47
  • @turkycat: That is basically what it is doing. `T` in the context of the `List` will be `MyType`. Maybe you could provide some examples of how `MyTypeList` would be used that would make the problem clearer. – Vaughn Cato Dec 02 '12 at 00:49
  • I'm sorry my intention is not more clear. Maybe this will help: Lets say that MyType has a `.process()` method... I want to be able to override getData() in the class `MyTypeList` so that I can have getData() manipulate the MyType object. I wouldn't be able to envoke a method on a wildcard type! Using my example, I could override getData() to have it `return data.process();` rather than `return data;` – turkycat Dec 02 '12 at 00:50
  • 1
    @turkycat: Maybe you are talking about a specialization so that `Node` is defined differently from `Node`s of other types. – Vaughn Cato Dec 02 '12 at 00:57