Skip to content

Cicularbuffer in function #42

Answered by rlogiacco
bergeronnext asked this question in Q&A
Discussion options

You must be logged in to vote

First of all, your question is not clear at all, so my answer is my best guess of what you might have been asking.

My guess then is you want to pass an instance of CircularBuffer to a function, which you can obviously do:

long sum(CircularBuffer<int, 100> &cb) {
  long result = 0;
  for (int i = 0; i < cb.size(); i++)
    result += cb[i];
  return result;
}

You can call such function as sum(buffer) where buffer must be an instance of CircularBuffer<int, 100>.

You can define a templated function if you put the following in a header file though:

template<size_t S>
long sum(CircularBuffer<int, S> &cb) {
  long result = 0;
  for (int i = 0; i < cb.size(); i++)
    result += cb[i];
  return r…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bergeronnext
Comment options

Answer selected by rlogiacco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants