🔒 Locked
Getting a Pointer Type
Advertisement
Original Post
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]
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]
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.
The function you''re probably thinking of is typeid(). As in:
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement