R
Rahul
Hi,
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast<A *>(p);
The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.
Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cast
The books suggests
static_cast=> "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cast=> To cast to a completely different meaning. The key
is that you'll need to cast back to the original type to use it
safely.
But I am not able to interpret the sentences in this context
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
Here p is passed to a function, which accepts void ptr. That function
need to cast it back
A *pp=static_cast<A *>(p);
The function is in the factory which accepts void *p only, the specific
implementations need to cast the pointer back to the expected class
and use it.
Question:Though both works fine, yet I want to know what is more
appropriate in this situation static_cast OR reinterpert_cast
The books suggests
static_cast=> "For "well-behaved" and "reasonably
well-behaved" casts,including things you might now do without a cast
reinterpret_cast=> To cast to a completely different meaning. The key
is that you'll need to cast back to the original type to use it
safely.
But I am not able to interpret the sentences in this context