Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Structures and Template

Status
Not open for further replies.

mteichta

ISP
Feb 12, 2002
9
0
0
NL
Hi all,

Im hoping someone might be able to assist.

If i pass a struct type to a template then i dont know the struct elements, right ?

how can i them create a sub routine to display elements of generic structs ?
 
You should show Your sample. struct and template are not so easy to use, and another compilers can do not the same with templates. If You use typedef struct, it may help You to work with templates.
Genarally, if You have a Pointer to a structure (in template's Function too), You can allways find out all elements of this structure.
 
thanks for your help.

but lets assume this following

struct test {
int value;
};

test data;
CQueue<test*> Queue;

Queue.Show(data);


template <class T> CQueue::Show(T* input) {

this function is unaware of the members of data hence i cannot do input.??? or input->???

You are right I could use a pointer but that would require me knowing the data types of member, which I only know at run time


}
 
Try do it so:
struct test {
int value;
};

template <class T>
class CQueue
{
public:
CQueue();
~CQueue();
void Show(T* input);
};

template <class T> CQueue<T>::CQueue()
{
}

template <class T> CQueue<T>::~CQueue()
{
}

template <class T> void CQueue<T>::Show(T* input)
{
input->value = 10; //Or what do You wish else
}

If You wish to use it, do it so:
test data;
CQueue<test> Queue;
Queue.Show(&data);

Is it that You wish?
 
Generally speaking, a template class CANNOT know the details of its template members. I've seen code, however, (in the C/C++ User's Journal magazind) that would allow you to create a template class that required the template parameter to have certain functionality. It's somewhat tricky, but really good stuff - I heartily recommend the magazine for serious coders.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top