[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] NS2 problem with Packet::datalen() function (packet.h)



> The current problem I have is that the AppData base class
> implementation of returns
>
> "sizeof(AppData)"
>
> So the current "int Packet::datalen()" function always returns
> "sizeof(AppData)" instead of the packet's "datalen()" ... which I

I don't understand.

        inline int datalen() const { return data_ ? data_->size() : 0; }

Here is Packet::datalen(), and data_ is:

        AppData* data_;         // variable size buffer for 'data'

And here is AppData::size():

        virtual int size() const { return sizeof(AppData); }

Sicne it's a virtual function, when you call it from a pointer to the base
class it'll automatically return whatever the actual value is of the
derived class.

I'm curious how did you test your code to know it always returns
sizeof(AppData)?

> believe was the intention of the "datalen()" method of the "Packet"
> class.  There are probably alternative (better) ways to remedy this
> situation ... Perhaps the type of the "data_" member of the Packet
> class should be of type PacketData instead of AppData? ...  Any
> suggestions for a better fix ... My current work-around in my own
> code is to access the Packet "data_" pointer (via the "userdata()
> method), cast is as type "PacketData" and get the PacketData size()
> ... This seems a faily non-intuitive approach as compared to using
> the Packet::datalen() method given some fix for it ...

I'm very confused. Since the size() function is virtual you do not need
this explict typecast, which is exactly what a virtual method is intented
to be...