Polymorphic Queue
        Posted  
        
            by metdos
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by metdos
        
        
        
        Published on 2010-04-08T09:21:29Z
        Indexed on 
            2010/04/08
            9:23 UTC
        
        
        Read the original article
        Hit count: 385
        
Hello Everyone, I'm trying to implement a Polymorphic Queue. Here is my trial:
QQueue <Request *> requests;
while(...)
    {
        QString line = QString::fromUtf8(client->readLine()).trimmed();
        if(...)){                      
            Request *request=new Request();
            request->tcpMessage=line.toUtf8();
            request->decodeFromTcpMessage(); //this initialize variables in request using tcpMessage
            if(request->requestType==REQUEST_LOGIN){
                LoginRequest loginRequest;
                request=&loginRequest;
                request->tcpMessage=line.toUtf8();
                request->decodeFromTcpMessage();
                requests.enqueue(request);
            }
            //Here pointers in "requests" do not point to objects I created above, and I noticed that their destructors are also called.
            LoginRequest *loginRequest2=dynamic_cast<LoginRequest *>(requests.dequeue());   
            loginRequest2->decodeFromTcpMessage();
        }
    }
Unfortunately, I could not manage to make work Polymorphic Queue with this code because of the reason I mentioned in second comment.I guess, I need to use smart-pointers, but how? I'm open to any improvement of my code or a new implementation of polymorphic queue.
Thanks.
© Stack Overflow or respective owner