Hello,
I'm new to templates in C++ and I'm having issues to port my c# code to c++
I want to create a circular buffer which can hold any type thus a template is required.
In c# I have this (simplified code)
public class buffer<T>;
{
private T[] items;
public buffer(int size)
{
items = new T[size];
}
}
also it's easy to use:
buffer<int> buf = new buffer<int>(10);
That creates a buffer from the type int that will be 10 long...
Anyway, so now I'm having problems porting this to unmanaged c++.
I've read a couple of tutorials on templates in c++ to get some sort of understanding, but I'm not able to work this out so far. It's probably very easy, but just a different syntax (I guess).
So my C++ code so far:
header:
template <class T>
class buffer {
public:
buffer(unsigned int size);
private:
T* items;
}
source file:
template <class T>
buffer<t>::buffer(unsigned int size)
{
items = new T[size];
}</t>
this compiles correctly, but I have now idea how to create a new buffer...
buffer<int> buf;
is not working, neither is the rest of what I came up with.
Can anybody tell me how to fix this in a neat way :)
Thanks in advance!