unsigned char *check = NULL;
check = (dynamic_cast<unsigned char *>( ns3::NetDevice::GetChannel() ));
This is what I am trying. But the error is:
error: cannot dynamic_cast ‘ns3::NetDevice::GetChannel() const()’ (of type ‘class ns3::Ptr<ns3::Channel>’) to type ‘unsigned char*’ (target is not pointer or reference to class)
I also tried:
reinterpret_cast
But it doesn’t work at all.
The return type of
ns3::NetDevice::GetChannel()is some kind of custom smart pointer; without seeing the definition of that, we can only guess at how you can convert that into a raw pointer.Perhaps it implements a conversion operator,
operator T*(), although that’s generally regarded as a bad idea since it makes it unintended conversions too easy to do by accident. In that case, you could do:Otherwise, perhaps it has a member function to convert to a raw pointer. The standard smart pointers conventionally call this
get():If it doesn’t offer that, and you really do want to get a raw pointer, then you could dereference it and take a pointer to the dereferenced object (assuming it supports dererencing; otherwise, it’s a bit odd to call it a pointer at all):
Once you have a
void *, you can usestatic_castto change it intounsigned char *, if that’s what you want. Be careful what you do with it, since messing around with the bytes of an object can easily lead to undefined behaviour.UPDATE: if
ns3::Ptris the template documented here, then you can get the raw pointer using: