Sign in to follow this  
Aidamina

Pointer problems

Recommended Posts

Why doesnt this work? And what do i need todo to make this work
float test()
{
    return 0.1337;
}
void * Call()
{
    return (void *)&test();
}

Share this post


Link to post
Share on other sites
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
void *? Yuck! What are you trying to achieve? There is a 99.9% chance that a better technique exists than void *s.

Σnigma

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
Any suggestions?
Its supposed to be a callback with result;
Cant i just return the result directly and ifso how do i turn that into void *
Thank you

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

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