• Advertisement

Archived

This topic is now archived and is closed to further replies.

Getting a Pointer Type

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The type in the examples given is resolved at compile time, so it isnt RTTI. Its like saying that overloaded function resolution is RTTI.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement