Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
public class buffer<T>;
{
	private T[] items;
	
	public buffer(int size)
	{
		items = new T[size];
	}
}

also it's easy to use:
C#
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:
C++
template <class T>
class buffer {
public:
	buffer(unsigned int size);
private:
	T* items;
}


source file:
C++
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...
C++
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!
Posted
Updated 19-Apr-23 7:22am
v5

First, don't use a separate source file for your template code - put it in the header file:
C++
template <class T>
class buffer {
public:
    buffer(unsigned int size)
    {
        items = new T[size];
    }
private:
    T* items;
};


There is no default constructor defined, so you need to supply the size when instantiating the class:
C++
buffer<int> buf(10);


...and don't forget to add a destructor to delete [] items at the end of its lifetime.
 
Share this answer
 
v3
Comments
wpte 30-Sep-11 5:52am    
I'm going to try this out... :)
Do you suggest that when I make other functions and methods that I should place them directly in the header file instead of the source file?
Graham Breach 30-Sep-11 5:58am    
For template classes and functions, yes.
wpte 30-Sep-11 6:08am    
Ah thanks,

I've just implemented everything in the header file and it works great!
Thanks for helping me writing better c++ code :)
It's just, in all the tutorials I read so far on templates, they all implement them in the source file instead of the header file :)
Stefan_Lang 30-Sep-11 6:17am    
You need a header file only if you want to share the functionality between different compilation units (i. e. .cpp files). Else you can just as well put your entire template class in the source file that needs it.

The thing with header files is, if you define a 'normal' class and want to share it, you only need to put the class declaration in the header file, and the implementation can go in the source file. If you want to share a template class (or function) though, you must put the implementations into the header file as well.
wpte 30-Sep-11 6:20am    
Makes sense, thanks :)
C++
//C++_Class_Templates.cpp

#include <iostream.h>
#include <vector>

template <typename t>
class MyQueue
{
     std::vector<t> data; 
   public:
     void Add(T const &);
     void Remove();
     void Print();
};

template <typename t> void MyQueue<t> ::Add(T const &d)
{
     data.push_back(d);
}

template <typename t> void MyQueue<t>::Remove()
{
     data.erase(data.begin( ) + 0,data.begin( ) + 1);
}

template <typename t> void MyQueue<t>::Print()
{
     std::vector<int>::iterator It1;
     It1 = data.begin();
     for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
        cout << " " << *It1<<endl;

}
//Usage for C++ class templates
void main()
{
     MyQueue<int> q;
     q.Add(1);
     q.Add(2);

     cout<<"Before removing data"<<endl;
     q.Print();

     q.Remove();
     cout<<"After removing data"<<endl;
     q.Print();
}
 
Share this answer
 
v9
Comments
wpte 30-Sep-11 5:43am    
Well I've implemented that...
But it still doesn't explain how to use a parameterized constructor in a template...
I mean... how can I say that MyQueue should be holding at most 10 T's?
Richard MacCutchan 30-Sep-11 5:44am    
Please use <pre> tags around your code to make it readable.
RKnGl 30-Sep-11 5:51am    
Sorry, besides being willing to help ,if I can, I am kinda ignorant towards most good practices regarding this site :( (have read most rules of a good conduct I've found) .. Will try to edit now ..
Richard MacCutchan 30-Sep-11 6:07am    
It's always worth playing around with the buttons at the top of the edit screen to see what they all do. You can even create a dummy question to practice on.
wpte 30-Sep-11 5:56am    
I appriciate your help RKnGI ;)
I think this page[^] will help to explain it.
 
Share this answer
 
C++
template<typename T, int N>
struct Buffer {
	using value_type = T;
	T data[N];
	constexpr int size() { return N; }
};
 
Share this answer
 
v6
Comments
Richard Deeming 20-Apr-23 4:10am    
This question was asked, answered, and solved almost twelve years ago. If you have something new to add to the discussion, then you need to explain it; just dumping an unexplained block of code isn't good enough.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900