Pointer "this" in class templates

Hello, I am trying to create template of something like list element. I wanted to overload operator + so it returns pointer to element this + n position "forward" on my list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T>
class LE{
public:
	T* prev;
	T* next;
.
.
.
	T* operator+(int n){
		T* result;
		result = this;
		for(int i=0;i<n;i++){
			if(result->nextLE() != NULL)
				result = result->nextLE();
			else 
				break;
		}

		return result;
	}
};


Then I create class LEP derived from LE:
1
2
3
class LEP: public LE<LEP>{
...
};


When I compile my code I have error in line 11:
error C2440: '=' : cannot convert from 'LE<T> *const ' to 'LEP *'
with
[
T=LEP
]

Why??
It seems to me that I can't use pointer this in class templates.
Is it true?
I need it in other templates too so, if it's true then I'm in trouble, if not please tell me how can I do that.
Last edited on
Let's suppose you instantiate LE as LE<int>.
On line 10 and 11, you're trying to assign this, which is an 'LE<int> *' to result, which is an 'int *'.
Thanks for reply.
You are right. It'll be completely mess when I'll use e.g. int but I never do that.
I solve my problem by changing line 11 like this:

result = reinterpret_cast<T*> (this);

Error was caused by my poor understanding of polymorphism.
What? No!
Change the type of 'result' to 'LE<int> *'. Don't use casting!
I can't correct you on the declaration of 'LEP'. Inheritance isn't one of my favorite features of C++, so I don't use it much. Even less when combined with templates. I can tell you, though, that that's not right.
Topic archived. No new replies allowed.