Should CleanupStack::PushL(CBase*) actually be CleanupStack::PushL(CBase*&)?

Login to reply to this topic.
Mon, 2005-08-29 18:11
Joined: 2005-06-13
Forum posts: 68
I'm sorry to post twice about the same topic.  Especially when there's probably a good reason why PushL takes a pointer by value and  not by reference (although I haven't thought of it yet).  If someone thinks I'm making a mountain out of a molehill please let me know Wink.

Cheers,

Nikolas.

If we fall down it's so we can learn to pick ourselves up.


Tue, 2005-08-30 23:04
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Should CleanupStack::PushL(CBase*) actually be CleanupStack:
Latter approach solves a different problem in C++. If you pass a pointer reference (e.g. CBase*&), then the function that got called can change the value of your pointer. This solution is often used, when you call a function from which you expect to properly initialize your pointer. Like this:

Code:
void Foo()
    {
    CSomethingBase* something = NULL;

    InitMyPointer( something );

    if ( something )
        {
        something->BeHappy();
        delete something;
        }
    }

void InitMyPointer( CSomethingBase*& aSomething )
    {
    // CSomething derives from CSomethingBase
    CSomething* something = new CSomething;    // It might be NULL
    aSomething = something;
    }

It's not the best example I've ever made, but perhaps you can see the point: a pointer reference is used for initialization in another function. As CleanupStack doesn't want to initialize the object, therefore it's enough if we pass a simple pointer.

Was I clear?

tOtE

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

Wed, 2005-08-31 01:17
Joined: 2005-06-13
Forum posts: 68
Re: Should CleanupStack::PushL(CBase*) actually be CleanupStack:
Thanks for replying Tote,

I'm aware of the semantics of passing a pointer by reference as opposed as by value, and the reasons you might want to do that as you pointed out.  You do make a fair point however that PushL does not initialize the pointer to a different object.  However consider this: I push a pointer to a CBase on the cleanup stack and then I make the pointer point to something else; when I pop the pointer off expliclitly then I'm going to get a panic.  Refer to my other post for an example.  I'm not saying it's necessary a good idea to do what I suggested but if PushL was to take a pointer by reference rather then by value then that would fix this problem.

Of course I might be horribly wrong Wink.

Thanks for your time,

Nikolas.

If we fall down it's so we can learn to pick ourselves up.

Wed, 2005-08-31 09:31
Forum Nokia Champion
Joined: 2003-10-01
Forum posts: 723
Re: Should CleanupStack::PushL(CBase*) actually be CleanupStack:
I commented the other posting.

Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/

  • Login to reply to this topic.