Call by Reference

I have the following function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyData  : public Singleton<MyData> {

template <class QUEUE_TYPE>
void MyData::dequeue(int name, QUEUE_TYPE *data) {
	QQueue<void*> *voidQueue = queues[name];   
	mutex[name].lock();
	while (voidQueue->isEmpty()) {
		queueNotEmpty[name]->wait(mutex[name]);
	}	
        // cout << *data  ---> outputs "test"
	data = reinterpret_cast<QUEUE_TYPE*> (voidQueue->dequeue());
        // cout << *data  ---> outputs the dequeued value
	queueNotFull[name]->wakeAll();
	mutex[name].unlock();		
}

QList<QQueue<void*> > queues;
QList<QMutex*> mutex;
QList<QWaitCondition*> queueNotEmpty, queueNotFull;
};


Now i call this function from outside this class to get an element from the queue.

1
2
3
QString* string = new QString("test");
MyData::instance().dequeue<QString>(0, string);
//  cout << *string  ---> outputs("test"); 


After this call string should point to the ellement that was dequeued in the function but it still points to the old one; When im debugging the function in a debugger the variable data in the function points to the dequeued element after the assigment in line 11. After the function returns, string points to the old one again.

What is wrong?!?
Last edited on
No. What you did was pass the object by reference. Not the pointer.
If you need to modify the pointer, make data be a 'QUEUE_TYPE **', and pass string as &string.

EDIT: By the way, make sure to delete the object after you're done with it.
Last edited on
Y I will test it tommorow. It makes sense when i think about it :)
Topic archived. No new replies allowed.