🔒 Locked
Pointer problems
Advertisement
Original Post
Store the temporary value returned by the first function in a heap-allocated variable, and return it. The caller is responsible for deleting that memory.
Check you favorite C++ manual for heap/stack allocation and temporary values.
float test() { return 0.1337;}void* call() { return new float(test());}Check you favorite C++ manual for heap/stack allocation and temporary values.
Below is the implented code. ret in the call function has a correct value in the local scope but it should have a value when it returns aswell if RT is a pointer right?
class ICallback { public: virtual void * Call()=0; }; template<class C,typename RT> class Callback : public ICallback { private: C* m_pClass; RT (C::*m_pMethod)(); public: Callback () { m_pClass = 0; return; } Callback (C* pClass, RT (C::*pMethod)()) { m_pClass = pClass; m_pMethod=pMethod; return; } void SetClass(C* pClass) { m_pClass = pClass; return; } void SetMethod(RT (C::*pMethod)()) { m_pMethod=pMethod; return; } void * Call() { RT ret = (RT )((m_pClass->*m_pMethod)()); return &ret } };
Is there a reason you can't also template the interface on return type?
The C++ type system is stronger than that in C. You are advised not to fight it - it wants to help you, but will not turn down a fight.
template <typename RT>class ICallback { public: virtual RT Call()=0;;// I suppose this qualifies as a variation on the// "curiously recurring template pattern"template <class C,typename RT>class Callback : public ICallback<RT> { // etc.The C++ type system is stronger than that in C. You are advised not to fight it - it wants to help you, but will not turn down a fight.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement