I'm a bit confused as to how to declare the object

Hello!

I'm trying to implement a circular linked list that uses arrays. I've written and tested my class known as "Buffer". This is the description of buffer class:

"The Buffer class implements a circular array buffer. The member variable m_buffer is a pointer to an array of integer values. The array will be defined by allocating memory dynamically. An object of Buffer class also contains the member variable m_next which is a pointer to another Buffer object. This member variable will be used by the BufferList class for implementation of the linked list."

My buffer class, as far as I know and from what I've tested, works correctly. Now I'm moving on to writing BufferList and I'm a bit stuck. I don't really fully understand what it means to create the object. I just want to know if I'm on the right track?

These are the instructions for the constructor:

Constructor creates a BufferList object which contains the first buffer of this object with the size minBufCapacity. If minBufCapacity is less than 1, the constructor creates a buffer with default size DEFAULT_MIN_CAPACITY. The constructor also initializes the member variables of the class.


This is what I've written:
1
2
3
4
5
6
7
8
BufferList::BufferList(int minBufCapacity){
    Buffer aBuffer(DEFAULT_MIN_CAPACITY); 

//member variables of class
    Buffer * m_cursor = nullptr;      
    int m_listSize = 0;         
    int m_minBufCapacity = 0;
}


Am I on the right track?
Last edited on
Hello drmario6532,

Based on the instructions I am thinking what you want here is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BufferList::BufferList(int minBufCapacity)
{
    if (minBufCapacity < 1)
    {
        Buffer aBuffer(DEFAULT_MIN_CAPACITY);
    }
    else
    {
        Buffer aBuffer(minBufCapacity);
    }

    //member variables of class
    Buffer * m_cursor = nullptr;
    int m_listSize = 0;
    int m_minBufCapacity = 0;
}

Without more to go on, the whole "Buffer" class and the "BufferList" class, this is more a guess for now, but seems right.

Andy
just to be clear this asks for a standard circular buffer, not a circular linked list.

a couple of tips:
mod will circle back...
int x[10];

for(int i = 0; i < 42; i++)
x[i%10] = i;
for(int i = 0; i < 10; i++)
cout << x[i] << endl; //it contains 42, 41, 40, 39, .....

and you may want to track current capacity and positions of data depending on how you read from it. typically you have the current reading location and the current writing location and usually read and write need to be in sync such that you don't read more than is in it or write new before some of the old was read. Getting them in sync can be tricky for some use cases. Its an aggravating data structure that works better in hardware and is sort of limiting as a software construct.
Last edited on
Topic archived. No new replies allowed.