0

If this is the original class:

class StorageBin
	   {
	      public:
		StorageBin();
		~StorageBin();
		void PutInBin( int Item, int Location);
		int ReturnFromBin (int Location);
		bool FindInBin(int Item);
		void DisplayBin();
	      private:
		int *pBin;
		int NumberInBin;
		int BinSize;
	   };

Would this be the correct conversion to make it a template?

Template <class T>
	class StorageBin
	   {
	      public:
		StorageBin();
		~StorageBin();
		void PutInBin( T Item, T Location);
		T ReturnFromBin (T Location);
		bool FindInBin(T Item);
		void DisplayBin();
	      private:
		int *pBin;
		int NumberInBin;
		int BinSize;
	   };

I ask because it compiles correctly, but as I have learned, that is not always the acid test

2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by Alex Edwards
Featured Replies
  • [QUOTE=henpecked1;631974]If you are referring to the first question, I know it compiles, but I also know that doesn't necessarily mean it's right, was just looking for a confirmation that it's correct for a template, or no, you're missing stuff.[/QUOTE] It's correct because you've generalized your list. That's one of the … Read More

0

Is my question really that dumb that I get nearly 30 views and no replies...lol

0

You answered your own question.

If you are referring to the first question, I know it compiles, but I also know that doesn't necessarily mean it's right, was just looking for a confirmation that it's correct for a template, or no, you're missing stuff.

If you are referring to the second question, that is an odd response coming from you, you're normally very polite and very helpful

1

If you are referring to the first question, I know it compiles, but I also know that doesn't necessarily mean it's right, was just looking for a confirmation that it's correct for a template, or no, you're missing stuff.

It's correct because you've generalized your list. That's one of the primary functions of templates - to make a general case for all valid objects/types.

Instead of only taking ints, it now takes anything that is a valid T parameter.

There are other reasons for templates, such as meta-programming and (supposedly) allowing a potential "bridge" for inheritance issues but from what I've heard the bridging isn't really a serious performance bump.

If you want an example of template meta-programming, consider the following code--

#include <cstdlib>
#include <iostream>

using namespace std;

class Fibonacci{
     public:
            template<int N>
            inline unsigned __int64 series(){   
                return (this->series<N - 1>() + (this->series<N - 2>()));
            }
};

template<>
inline unsigned __int64 Fibonacci::series<1>(){
    return 1;
};

template<>
inline unsigned __int64 Fibonacci::series<0>(){
    return 0;
};

int main(int argc, char *argv[]){
    const int n = 6;
    Fibonacci fib;
    cout << fib.series<n>() << endl; //prints out the nth result of the Fibonacci series
    cin.get();
    return 0;
}
Comments
Alex was, as usual very helpful and very kind in his replies
0

Thank you Alex, as usual, you are the soul of courtesy, my apologies if you took any offense to the second comment.

0

Thank you Alex, as usual, you are the soul of courtesy, my apologies if you took any offense to the second comment.

No offense taken. My response wasn't fairly directed so the reply is understandable.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.