1

I will form the question better through an example.

I want to extend the defult implementation of Qtcpsocket the following way:

class NumberedTcpSocket : public QTcpSocket
{
Q_OBJECT
public:
    NumberedTcpSocket(QObject *parent=0);
    int number;
};

The only thing i'm doing here is adding a int number field to the default implementation of QTcpSocket. I need this to enumerate opened sockets.

However, the rest of the networking classes work with, or return a Qtcpsocket. It doesn't matter that the change i did is small.

I would like to do something like this:

NumberedTcpSocket *clientConnection = returnsPointerToQTcpSocket();
clientConnection->number = predefined_number;

What kind of casting needs to be done in this situation? Does it include a change in the constructor of the NumberedTcpSocket?

Peter Bloomfield
  • 5,578
  • 26
  • 37
TheMeaningfulEngineer
  • 15,679
  • 27
  • 85
  • 143

2 Answers2

2

What's not clear from your question is whether you're the one creating the socket object. If it is constructed with new NumberedTcpSocket(), then you can cast from QTcpSocket using dynamic_cast. If not, well, it isn't a NumberedTcpSocket and it's not possible to treat it as one.

An alternative, if all you need to do is associate a number with the socket, is to avoid creating a subclass and use the QObject dynamic property system. That is, you can set the number with clientConnection->setProperty("number", predefined_number);, and retrieve it with clientConnection->property("number").toInt();. But it's hard to advise without more detail about what you're trying to achieve.

Dan Milburn
  • 5,600
  • 1
  • 25
  • 18
  • The `NumberedTcpSocket` is never created explicitly. I only have a pointer to a `QTcpSocket` from `returnsPointerToQTcpSocket()`. I wanted to to cast `*QTcpSocket` to `*NumberedTcpSocket`. And by the casting make all the atributes (initialized and set by `returnsPointerToQTcpSocket()`) and methods from `QTcpSocket` available plus the `number` attribute. – TheMeaningfulEngineer Oct 24 '13 at 18:03
1

Let me recap to make sure I understand: You want to add something to the sockets that you created yourself? Or you want to add something to the sockets created by Qt? That latter is not possible (Qt only reserved space for how much it needed for QTcpSocket, where would it store your additional number)?

In the former case, you'd need a dynamic cast. Make sure also to check if the result is != 0, to see if the cast was successful:

NumberedTcpSocket *clientConnection = dynamic_cast<NumberedTcpSocket*>
    (returnsPointerToQTcpSocket());
if (clientConnection)
{
    clientConnection->number = predefined_number;
}

But the question is why you didn't already create the sockets with the appropriate number from the beginning then.

A more robust solution, in both cases, would probably be to have a map where you store for each socket this number you need:

QMap<QtcpSocket*, int> m_socketNumbers;
codeling
  • 11,056
  • 4
  • 42
  • 71
  • @vahancho from all I know about dynamic casts they can do both, but special case for base-to-derived (on pointer) is that they will return 0 when not really a derived object is pointed to (see e.g. http://stackoverflow.com/questions/2253168/dynamic-cast-in-c) – codeling Oct 23 '13 at 10:38
  • I took my comment back after making sure that QTcpSocket is a polymorphic class - in that case base-to-derived makes sense. – vahancho Oct 23 '13 at 10:40