[Ns-developers] A question about downcast netdevice pointer.
Gustavo Carneiro
gjcarneiro at gmail.com
Thu Sep 25 09:16:22 PDT 2008
2008/9/25 Nan Li <kido.ln at gmail.com>
>
> Thanks your answer.
> I just have a little confuse about this.
>
> dynamic_cast is a straight thought about converting base
> class(netdevice) pointer to child class (csmanetdevice) pointer, which
> means there is only one object.
>
> "aggregated" seems there are two objects aggregated together. Why
> there is CsmaNetDevice object aggregated with a NetDevice object?
>
> I went throught the code, it seems there is only one object
> (CsmaNetDevice) for each node.
>
> Am I right?
You are right, there is only one object.
I for one find using GetObject<SubClass>() for downcasts less intuitive than
dynamic_cast, but I am aware some other developers don't quite agree with me
for whatever reason.
Although dynamic_cast<CsmaNetDevice*> (PeekPointer (dev)) is a bit more
verbose. Ideally I think it would make a lot of sense to have a
smart-pointer based dynamic cast variant, something like:
static inline Ptr<T2> DynamicCast<T2> (Ptr<T1> ptr)
{
return Ptr<T2> (dynamic_cast<T2*> (PeekPointer (ptr)));
}
Unfortunately the idea got no traction, at the time.
>
>
> Thanks,
>
>
>
> On Thu, Sep 25, 2008 at 1:05 AM, Tom Henderson <tomh at tomh.org> wrote:
> >
> > kido wrote:
> >> Hi,
> >>
> >> I am new user of ns3. When I try to downcast the netdevice pointer, I
> >> got error message.
> >>
> >> I use the example file of realtime-udp-echo.cc What I did is what to
> >> get the one of CsmaNetDevice pointers.
> >>
> >> Ptr<Node> node = n.Get(2);
> >> Ptr<NetDevice> dev = node->GetDevice(0);
> >> if (dynamic_cast<CsmaNetDevice*>(PeekPointer (dev))){
> >> .................
> >> }
> >>
> >> When I compile the program, I received the compile error for the
> >> dynamic_cast as:
> >>
> >> ../scratch/emulator.cc:149: error: cannot dynamic_cast
> >> 'ns3::PeekPointer [with T = ns3::NetDevice](((const
> >> ns3::Ptr<ns3::NetDevice>&)((const ns3::Ptr<ns3::NetDevice>*)(&
> >> dev))))' (of type 'class ns3::NetDevice*') to type 'struct
> >> ns3::CsmaNetDevice*' (target is not pointer or reference to complete
> >> type)
> >>
> >> How should I do this downcast? I borrow the code from
> >> csma-net-device.cc. I don't understand why the code in that file is no
> >> problem.
> >
> > Another way you can do this downcast (although in looking at our
> > documentation tonight, it is not very well documented in either the
> > tutorial or manual) is to use the GetObject method:
> >
> > Ptr<Node> node = n.Get(2);
> > Ptr<NetDevice> dev = node->GetDevice(0);
> > Ptr<CsmaNetDevice> csmaDev = dev->GetObject<CsmaNetDevice> ();
> >
> > This queries the NetDevice object to see if it has a CsmaNetDevice
> > interface aggregated to it. But if you have the dynamic_cast method
> > working, it should be more or less equivalent.
> >
> > Tom
> >
> > >
> >
>
>
>
> --
> Thanks,
> Nan Li
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups
> "ns-3-users" group.
> To post to this group, send email to ns-3-users at googlegroups.com
> To unsubscribe from this group, send email to
> ns-3-users+unsubscribe at googlegroups.com<ns-3-users%2Bunsubscribe at googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/ns-3-users?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>
--
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