View Full Version : accessing a templated types methods
EscapeCharacter
11-25-2002, 09:38 PM
say ive got this template class called Node which is used in a linked list thingy. and the class has methods to set the next pointer.
i would declare it
Node<MyType> *t = new Node<MyType>;
t->SetNext(NULL); //accesses the method SetNext of the Node class
t->GetSize(); // method of MyType doesnt work.
how would i go about making that work, the only thing i could think of was have Node inherit the behavior from MyType and was wondering if there was another/better way to do it.
bwkaz
11-26-2002, 02:16 PM
You'd use something like t->GetData()->GetSize(); -- where GetData returns a pointer (or reference, either way) to the internal object that the node is keeping track of.
If you declare it inside a "template<class T>", then make the GetData() function return a T * or a T&.
At least, I'm fairly sure that will work...
EscapeCharacter
11-26-2002, 07:57 PM
cool ill have to try that
EscapeCharacter
11-30-2002, 04:56 AM
well this doesnt seem to be working out the way i had thought, i create that GetData function but im getting weird results.
T *SNode::GetData(){
return (T*);
}
im accessing the methods like you showed above t->GetData()->GetSize(); but something weird is happening. i have this base class that all my other class share and it has alot of generic methods, and those seem to work fine with the above code. but i try using a method from the class it self i.e. not inherited from the base class my app segfaults. ill post the relevant code for all of this later. just wondering if anyone has any thoughts on why that would happen
bwkaz
12-01-2002, 08:45 PM
T* is a type, not an object. ;)
Post the SNode class declaration (i.e. what's in the .h file), I think that will help.
EscapeCharacter
12-02-2002, 07:07 PM
ok here it is
#ifndef __SNODE__
#define __SNODE__
#include <iostream>
template<class T>
class SNode{
public:
SNode();
T *GetData();
void SetNext(SNode *);
void SetPrev(SNode *);
private:
SNode *next;
SNode *prev;
};
template<class T>
SNode<T>::SNode(){
next = NULL;
prev = NULL;
cout << "Createing template class SNode" << endl;
}
template<class T>
T *SNode<T>::GetData(){
return (T*) this;
}
template<class T>
void SNode<T>::SetNext(SNode *n){
next = n;
cout << "Setting next SNode" << endl;
}
template<class T>
void SNode<T>::SetPrev(SNode *p){
prev = p;
}
#endif
stuka
12-02-2002, 07:40 PM
I see your problem now! You need another variable, like so: template<class T>
class SNode{
public:
SNode();
T *GetData();
void SetNext(SNode *);
void SetPrev(SNode *);
private:
SNode *next;
SNode *prev;
T* data;
};
and then you do this:template<class T>
T *SNode<T>::GetData(){
return data;
}
EscapeCharacter
12-02-2002, 08:05 PM
heh thats actually funny cause i was gonna try that out as my next test. woot now i can fix this mofo, thanks :)
edit:
something im still a little confused about is how would i set data?
ive tried using data = this; but that seems to be wrong, im getting
g++ -c sframe.cpp -O6 -Wall -DDEBUG -g
snode.h: In constructor `SNode<T>::SNode() [with T = SImage]':
sframe.cpp:10: instantiated from here
snode.h:23: cannot convert `SNode<SImage>* const' to `SImage*' in assignment
snode.h: In constructor `SNode<T>::SNode() [with T = SMenuBar]':
sframe.cpp:19: instantiated from here
snode.h:23: cannot convert `SNode<SMenuBar>* const' to `SMenuBar*' in
assignment
make: *** [sframe.o] Error 1
when compiling
bwkaz
12-02-2002, 08:47 PM
You either provide functions for your users to set it, or you make data public.
data is what stores the, well, data, that this node is taking care of. For example, if you have a binary tree to keep track of, say, process IDs in a kernel (or something, that isn't a great example, but I can't think of anything else ATM), then *data will store your process ID. You'd do something like this in the user code:
// .... stuff before this...
SNode<pid_t> *tree = new SNode<pid_t>(new SNode<pid_t>(NULL,NULL), NULL);
tree->data = new pid_t(pid1);
tree->left->data = new pid_t(pid2);
// etc., etc. Then e.g. tree->left->GetData() would return a pointer to pid2.
Of course, that's only the quick-and-dirty way to do it. Proper OO design would dictate that you add a SetData(T *newdata) function that sets data = newdata. Then, instead of doing tree->data = new pid_t(pid1), you'd do tree->SetData(new pid_t(pid1));, and tree->left->SetData(new pid_t(pid2)) or something similar.
Edit: Linked lists instead of binary trees. Hmm, well, most of the example would still work. :redfaced:
EscapeCharacter
12-02-2002, 09:12 PM
i dont fully understand all that but ill have to give it a try when i get home tomorrow, thanks for your quick response.
stuka
12-03-2002, 11:15 AM
Well, here's a brief example:
template<class T>
void SNode<T>::SetData(T* newData){
data = newData;
}
And if you wanted to get really tricky, you could do something like:
template<class T>
SNode<T>& SNode<T>::operator=(T* rhs){
data = rhs;
return *this;
}
<edit>Fixed it to make nitpicky people happy :P</edit>
bwkaz
12-03-2002, 12:55 PM
Wouldn't you want to "return *this" instead of just "return this"?
;)
EscapeCharacter
12-23-2002, 05:05 PM
heh i finally got around to doing this but its all working now, i love you guys :)
stuka
12-23-2002, 05:12 PM
ssshhhhh! not so loud! somebody might hear!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.