Go Down

Topic: C++ templates (Read 17815 times) previous topic - next topic

aarondc

I am trying to develop a linked list of structures containing different types.

I have read quite a few different websites' content regarding C++ templates. I am finding the examples and explanations either too simplistic, or overly involved explanations, but still simplistic examples.

So I am doing a Goldilocks and the three bears and asking if anyone can suggest a good resource (URL or ebook) that is not too heavy, and not too light, but just right.

I learn far better from being hands on, so am playing with the concept in some code I am developing, but a good intro that develops into a full-blown example would be appreciated.

I have code working via hand-coded functions and a hard coded array of struct. I went from there to a multi-class with inheritance and class and function templates solution that naturally produced some interesting but nonetheless unhelpful results. I am now going back to square 1 and attempting to develop the template + class solution step by step, iteratively. So the search for a good template intro + examples + development of example resumes.

Any suggestions?

Thanks in advance.

aarondc

Of course. Having had to describe my problem (only one of them ;)) affords me a useful search expression to use, and I am turning up helpful links via the expression, "linked list of different data types". I honestly did not expect such a narrow search term to be useful. Glad to be proven wrong.

So thanks for being here, forum, you've helped me already :D

nickgammon

It's a little unusual to have a linked list of different types. Why do you require this? If the different types are small you could use a union. Failing that a pointer to a union. The standard template library (STL) does a very good implementation of linked lists (amongst a lot of other things).

http://www.gammon.com.au/forum/?id=11119
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

aarondc

I'm already using a union with my hand-coded functions, so that solution is up and running. Rather than use a union, I wanted to use templates. Rather than use an array (of compile-time fixed size), I wanted to create a linked list. That's the progression of my thought process, leading me to a linked list of different types.

I'll check out STL (thanks).


aarondc

What I'm really looking for is a template tutorial. Regardless of whether I can do what I want here, templates are going to be useful.

nickgammon

Templates let you share functionality between different types. For example (in the STL and other places) you can write a single sort function that will sort multiple types, because the sort is templated. Similarly for linked lists and so on.

However the "multiple" refers to being able to have one function that works with multiple types, one at a time. It doesn't mean a single list of multiple types, but multiple lists of a single type.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

aarondc

Yeah. Which is why I want a good template tutorial. Because words are not as good as pictures, which are not as good as actually programming something, hands on, when it comes to my learning style.

Hence the request for a good template tutorial resource.

If I find one I'll post it here.

aarondc

I'm muddling my way through. I have no idea how I would be able to do this pre-internet. What a brilliant learning tool.

PaulS

Quote
I have no idea how I would be able to do this pre-internet.

There used to be things called libraries...  8)
The art of getting good answers lies in asking good questions.

aarondc

#9
Jun 20, 2013, 01:05 pm Last Edit: Jun 20, 2013, 01:09 pm by aarondc Reason: 1

Quote
I have no idea how I would be able to do this pre-internet.

There used to be things called libraries...  8)


Pretty sure they were shut by 9pm. Rarely saw the latest programming books in 'em either.
And a book rarely has an example as varied as the ones you find online.

PaulS

Quote
Pretty sure they were shut by 9pm.

Required planning. What a concept!

Quote
Rarely saw the latest programming books in 'em either.

But, they could be obtained with an inter-library loan. Yeah, I know. Requires you to understand ALL the capabilities of the library system.

Quote
And a book rarely has an example as varied as the ones you find online.

But, it covers the concepts. Application of the concepts is up to the researcher.
The art of getting good answers lies in asking good questions.

aarondc


Quote
Pretty sure they were shut by 9pm.

Required planning. What a concept!


Consistent condescending belligerence and sarcasm from a forum oldbie, what a concept!

Quote

Quote
Rarely saw the latest programming books in 'em either.

But, they could be obtained with an inter-library loan. Yeah, I know. Requires you to understand ALL the capabilities of the library system.


Not if they weren't anywhere in the library system. Yes, I know. An inter-library loan required that the book had been bought, entered into the sytem, and was not currently on loan and did not have any holds on it. A less than infinte budget means guess what!? Not every single book was available. ZOMG!! Imagine that!!


Quote

Quote
And a book rarely has an example as varied as the ones you find online.

But, it covers the concepts. Application of the concepts is up to the researcher.


It might cover the concepts. It's not guaranteed though.

The Internet trumps the library. Hands down.

aarondc

Now then. How brilliant are void pointers!?

aarondc

I now have an array of base class pointers that act like any sort of type via template class and function definitions, with a little bit of polymorphism, virtual method and void pointer manipulation thrown in for good measure.

I should be able to go from this point to linked lists, so my array does not need to be defined at compile time to a fixed length.

Tomorrow. :D

aarondc

I have absolutely no idea what I am doing, but it works for my currently very limited test suite, and that's the main thing:

Code: [Select]

class baseClass {
   protected:
       bool isDone;
       bool isPin;
   public:
       baseClass() {isDone = false;};
       virtual void debug() {};
       virtual bool isEqual(void *thisVarPtr) {};
};


template <typename T1, typename T2>
class childClass :  public baseClass {
   private:
       T1 variableName;
       T2 lastValue;
       T2 *variablePtr;
   public:
       childClass(T1 thisVariableName, T2 &thisVariable, bool thisIsPin = false) {
           isPin = thisIsPin;
           variableName = thisVariableName;
           variablePtr = &thisVariable;
           lastValue = thisVariable;
       };
       virtual void debug() {
           if ((!isDone) || (*variablePtr != lastValue)) {
               isDone = true;
               lastValue = *variablePtr;
               Serial.print(variableName);
               Serial.print(" == ");
               Serial.println(*variablePtr);
           }
       };
       virtual bool isEqual(void *thisVarPtr) { return (thisVarPtr == variablePtr); };
};

template <typename T1, typename T2>
baseClass *newChild(T1 thisVariableName, T2 &thisVariable) {
   return new childClass<T1, T2>(thisVariableName, thisVariable);
};

int varCounter = 0;
baseClass *bArray[10];

template <typename T>
void debug(T &thisVariable) {
   for (int i = 0; i < varCounter; i++) {
       if (bArray[i] -> isEqual(&thisVariable)) {
           bArray[i] -> debug();
           break;
       }
   }
};


Go Up