Getting a Pointer Type

Started by
7 comments, last by 4 19 years, 11 months ago
If you have a pointer, there is some function that will give you what type it points to. It is a simple function but I do not remember it. char *kill; kill = new SomeFunction(kill); // kill = new char; Does anybody know what it is?
Advertisement
Something like this? Its a bit hackish..

template<typename T>T* allocate(T*){	return new T;}int main(){char* p;p = allocate(p);delete p;}


[edited by - Jingo on May 8, 2004 12:29:54 PM]
quote:Original post by 4
If you have a pointer, there is some function that will give you what type it points to.

Yup! Welcome to the wonderful world of metaprogramming!

template< typename Type >struct dereferenced_type;template< typename DereferencedType >struct dereferenced_type< DereferencedType* >{  typedef DereferencedType type;};int main(){  dereferenced_type< char* >::type a; // a will be of type char}
From A Conversation with Scott Meyers:

When to Use RTTI

Bill Venners: You said in the first edition of Effective C++:

Fortunately, C++ provides no way to determine at runtime the actual type of object a pointer points to, and let me assure you this is no accident. In fact, runtime type inquiry (RTTI) was explicitly omitted from the language to prevent abuses such as this.

Doesn't C++ have RTTI now?

Scott Meyers: Yes.

Bill Venners: When is it appropriate to use RTTI?

Scott Meyers: Let me give you some history on why the committee added RTTI to the C++ language. This is one of those cases where the consumers won. At the time, every non-trivial class library being written was rolling its own RTTI, without exception. Every major class library used at the time, including MFC (Microsoft Foundation Classes) and NIHCL (National Institute of Health Class Library), had RTTI of some form or another. So the committee just said, "Rather than have N different ways to accomplish the same thing, we're going to have a single language mechanism that does the right thing." That's why RTTI was added. It was a simple matter of people wanted it.

Once the committee added RTTI, people started wondering when it was appropriate to use RTTI. All the bad things I wrote about RTTI in the first edition of Effective C++ continue to apply. They're still bad. So you end up with a classic engineering tradeoff. You use RTTI when the alternatives are worse.



[edited by - JohnBolton on May 8, 2004 1:58:10 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
But this isnt RTTI
quote:Original post by Jingo
But this isnt RTTI


If you want to know what SomeFunction returns, just look at its declaration. Otherwise, your are using RTTI and (assuming C++) the operator typeid is probably what your are looking for.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
The type in the examples given is resolved at compile time, so it isnt RTTI. Its like saying that overloaded function resolution is RTTI.
The function you''re probably thinking of is typeid(). As in:

const char* name = typeid(UnknownPointer).name(); 


But like the above posters mentioned, you need to enable RTTI in the compiler to use it, and it''s best to avoid using it if you can.
This is not RTTI as Jingo pointed out.

This topic is closed to new replies.

Advertisement