CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Why Pointer to Pointer?

    Hi, My problem is "Why"
    following is the code i am working on.
    class AbstractBase
    {
    public:
    virtual void funct() = 0;
    };
    class Child : public AbstractBase
    {
    void funct()
    {
    printf("This is Child function\n");
    }
    };
    void RetFunctPtr(AbstractBase * recvPtr)
    {
    recvPtr = new Child;

    }
    the main contains this
    AbstractBase * sendPtr;
    RetFunctPtr(sendPtr);
    sendPtr->funct();// memory error......

    This does not let me access the desired function that is child's funct.

    The solution to the above problem is :
    class AbstractBase
    {
    public:
    virtual void funct() = 0;
    };
    class Child : public AbstractBase
    {
    void funct()
    {
    printf("This is Child funct\n");
    }
    };
    void RetFunctPtr(AbstractBase ** recvPtr)
    {
    *recvPtr = new Child;
    recvPtr->funct();
    }
    the main contains this
    AbstractBase * sendPtr;
    RetFunctPtr(&sendPtr);
    sendPtr->funct(); //Ok let me access

    Spread Love and Knowledge.
    Spread Love And Knowledge

  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Why Pointer to Pointer?

    http://groups.google.com/groups?q=C%...siq.com&rnum=5

    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    igor@soukhov.com | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Re: Why Pointer to Pointer?

    Your site address does not solve the problem at all. I need to know why cannot "Just" the Abstract class pointer in my example be used? instead of pointer to pointer to the class (in the solution).

    Spread Love and Knowledge.
    Spread Love And Knowledge

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why Pointer to Pointer?

    parameters are passed by value unless you use the reference specifier which is &. You can pass by address, so you are slightly confused because you are passing a pointer, so you are thinking that you are passing by address.

    However, you can use a pointer as a "pass by address" parameter when it is the memory that the pointer is pointing to that you want to change.

    In your case, it is the pointer itself you want to change. So you need to either pass it by reference or (as you did in your solution) by address, i.e. the address of the pointer.

    Your code will not work by the way, because this is wrong:


    void RetFunctPtr(AbstractBase ** recvPtr)
    {
    *recvPtr = new Child; // one way to set the parameter
    recvPtr->funct(); // incorrect.
    }




    you must instead do

    (*recvPtr)->funct();



    Note: you need the bracket because -> has higher precedence than *

    However you could have the calling function make the call to funct().
    The option using a reference is below:


    void RetFunctPtr( AbstractBase* & rectPtr )
    {
    rectPtr = new Child; // as before, but now it will work
    // rectPtr->funct(); // it will work to call it from here like this
    }



    However why not simply use the return value?

    AbstractBase* RetFunctPtr()
    {
    return new Child;
    }



    Note that as you are calling new you need to be careful with memory management. Firstly you have to remember to delete the pointer later, secondly you have to ensure that the parameter you are setting is not already allocated and holding the last reference to the pointer.

    Shared smart-pointers are usually the best way to look after this situation.


    The best things come to those who rate

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why Pointer to Pointer?

    read my post, it explains it.


    The best things come to those who rate

  6. #6
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Re: Why Pointer to Pointer?

    Thanks! Thanks a lot for the wonderful reply! I have understood now what was lacking... and sorry that i forgot to remove the wrong code in my solution. I shall verify a couple of things and shall post again if there still is any doubt.

    Spread Love and Knowledge.
    Spread Love And Knowledge

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured