Cast object pointer to char array

unsigned char *check = NULL;
check = (dynamic_cast<unsigned char *>( ns3::NetDevice::GetChannel() ));

This is what I'm 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 this does not work at all.

+3
source share
2 answers

The return type ns3::NetDevice::GetChannel()is a kind of customizable smart pointer; without seeing the definition of this, we can only guess how you can convert this to a raw pointer.

It may implement the conversion operator operator T*(), although this is usually considered a bad idea, as it makes unintentional conversions too easy to perform by accident. In this case, you can do:

void * check = ns3::NetDevice::GetChannel();

, , - . get():

void * check = ns3::NetDevice::GetChannel().get();

, , ( , , , ):

void * check = &*ns3::NetDevice::GetChannel();

void *, static_cast, unsigned char *, , . , , undefined.

UPDATE: ns3::Ptr , , :

void * check = PeekPointer(ns3::NetDevice::GetChannel());
+1

static_cast reiterpret_cast. , . , void* , ( ).

unsigned char *check = NULL;
check = static_cast<unsigned char*>(static_cast<void*>(ns3::NetDevice::GetChannel()));

Ptr<Channel> , :

template<typename T>
class Ptr
{
public:
  operator T*() {return _p;}

private:
  T* _p;
};
0

All Articles