Sign in to follow this  

Pointer problems

This topic is 4302 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

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.


float test() {
return 0.1337;
}

void* call() {
return new float(test());
}


Check you favorite C++ manual for heap/stack allocation and temporary values.

Share this post


Link to post
Share on other sites
Well the think is that it should no only return float this was just an example, i want to return an object aswell so it should be some kind of universal pointer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the return value that holds the 0.1337 is destroyed, so what Call() returns is useless you need to store it in something permenant like ToohrVyk suggested, if you using c which what it looks like you can use malloc()

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Aidamina

void * Call()
{

RT ret = (RT )((m_pClass->*m_pMethod)());

return &ret;
}


You return the address of a stack variable, which is destroyed when the function returns, so you return the address of an invalid variable.

Share this post


Link to post
Share on other sites
Is there a reason you can't also template the interface on return type?


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.

Share this post


Link to post
Share on other sites

This topic is 4302 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this