[Ns-developers] A question about downcast netdevice pointer.

Gustavo Carneiro gjcarneiro at gmail.com
Fri Sep 26 03:35:22 PDT 2008


(wiping out all replied to text to make the email shorter)

(with fear of triggering more silly API breakage... :-/ )

Craig, consider this:

class Csma : NetDevice;
class Wifi : NetDevice;

Now you create a mutant netdevice object instance with Csma aggregated:

Ptr<Wifi> wifi = CreateObject<Wifi> ();  // 0x123 address
Ptr<Csma> csma = CreateObject<Csma> ();  // 0x456 address
wifi->AggregateObject (csma);

Now, given the 'wifi' pointer, what should wifi->GetObject<NetDevice> ()
return?  0x456 or 0x123?  They are both NetDevices...  I know this example
doesn't make much sense, but I am still convinced GetObject () is wrong for
type casting.

If I understand what you're saying, you think it would be simpler if
developers pretended NetDevice and CsmaNetDevice were two separate objects
which are aggregated.  I emphatically disagree.  Developers *must know* that
NetDevice is a simple base class for CsmaNetDevice, not just another
object.  If the developer was not supposed to know it then he would be
writing lots of GetObject () calls before calling any NetDevice method;
clearly silly and a runtime penalty.  And it would be more complicated than
it needs to: inheritance is a concept all C++ programmers already know and
are used to, there's no point in taking it away from them.

On the other hand I am not sure I agree with Mathieu's dismissal of the type
system's importance.  Sure, we may not have 'pure interfaces' in the
COM/Java sense, but we have an API that allows registering and querying for
what I'd call 'orthogonal features', or 'traits'.  The traits are provided
by classes.  Whether the classes are abstract (interfaces) or not (objects)
only matters (slightly) for those programmers trying to implement one such
feature.  From the point of view of a simple simulation script writer it
doesn't matter if he is dealing with interfaces or objects, it's all the
same.  This is vaguely similar to Aspect Oriented Programming, and where
would NS 3 be without it... *shudder*

Just my 2 cents,

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert


More information about the Ns-developers mailing list