Ray Foo Ray Foo - 5 months ago 16
MySQL Question

c++ - Heap object member function access

I am not quite sure which question I should be asking so I am gonna go with where the errors pop up.

I am working on a program to stream tick data from Interactive Brokers into a MySQL DB. To remove any bottlenecks that may occur from the difference between stream dl speed and MySQL write speed, I am trying to implement a queue class that will store Tick objects temporarily. Code:

TickQueue::TickQueue()
{
tickQueue = new std::queue<Tick>;
}

TickQueue::~TickQueue()
{
delete tickQueue;
}

void TickQueue::add(Tick t)
{
tickQueue->push(t);
}

int main()
{
time_t dt = time(0);
struct tm currTime;
localtime_s(&currTime, &dt);
TickQueue tq;

for (int i = 0; i < 5; ++i)
{
Tick tTick("fx", 1.2151, "SYM", i, currTime); tq.add(tTick);
}

if (tq.size() == 5) { cout << "success" << endl; }



return 0;
}


To keep it persistent, I decided to initialize it on the heap. However, I have a feeling this may not be the best implementation.

I also cannot access tq.size() as says size() is not a member function of tq. Is there a way to Construct a new object and have it inherit the members of its underlying structure (std::queue in this case)

Answer

When you declared TickQueue, you put a member inside it like this:

std::queue<Tick> *tickQueue;

That is, you were saying that TickQueue 'has-a' queue<Tick> inside it.

Instead, declare TickQueue like this:

class TickQueue : public std::queue<Tick> {
}; // TickQueue

Now you're saying that TickQueue 'is-a' queue<Tick>, and inherits all the methods thereof.