Dynamic casting to ns3::Ptr<ns3::PointToPointNetDevice> failing.

1,028 views
Skip to first unread message

Anil Jangam

unread,
Jan 15, 2016, 3:10:11 AM1/15/16
to ns-3-users
HI,

I am trying to perform this dynamic casting but it is failing with the following error. I am not sure what is the error here?

ns3::Ptr<ns3::PointToPointNetDevice> netDevice_0 = dynamic_cast<ns3::Ptr<ns3::PointToPointNetDevice>>(node->GetDevice(0    ));

Compile error:
error: cannot dynamic_cast ‘ns3::Node::GetDevice(uint32_t) const(0u)’ (of type ‘class ns3::Ptr<ns3::NetDevice>’) to type ‘class ns3::Ptr<ns3::PointToPointNetDevice>’ (target is not pointer or reference)
   ns3::Ptr<ns3::PointToPointNetDevice> netDevice_0 = dynamic_cast<ns3::Ptr<ns3::PointToPointNetDevice>>(node->GetDevice(0));

The GetDevice() returns the type:  Ptr<NetDevice> GetDevice (uint32_t index) const; So guess I am doing the type casting according to the target data type. Is it something to do with the ns3::Ptr<T>? Also, the target type is of type ns3::Ptr<T> so I am not sure why the error message is saying hat target is not pointer or reference.

Can someone please help me understand why is this and possibly fix this?

Regards,
/anil.

Tommaso Pecorella

unread,
Jan 15, 2016, 4:25:09 AM1/15/16
to ns-3-users
hI,

dynamic cast doesn't work on pointers... or better, it could, but Ptr is different: it's a template.
Usually you use DynamicCast, and on instances, not on their Ptr.

Hope this helps,

T.

Anil Jangam

unread,
Jan 15, 2016, 4:37:24 AM1/15/16
to ns-3-...@googlegroups.com

Hi Tommaso,

Thanks for reply. I changed it as below and it worked.

ns3::PointToPointNetDevice *netDevice_0 = dynamic_cast<ns3::PointToPointNetDevice*>(&(*(node->GetDevice(0))));

But do you think if there is a better way of writing it? This looks very untidy.

/anil


--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/0FdYvZRvPlA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

Tommaso Pecorella

unread,
Jan 15, 2016, 10:42:07 AM1/15/16
to ns-3-users
Hi,

yes, the paint is that you shouldn't really be working with pointers to NetDevices. Or better, the need for a dynamic cast of NetDevice pointers have never been really foreseen.
Question is: are you sure you need it ?

Cheers,

T.
Reply all
Reply to author
Forward
0 new messages