Pass pointer to function

This is a discussion on Pass pointer to function within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to pass a pointer to a function that accepts a reference to an object. Can someone ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Pass pointer to function

    Hi,

    I am trying to pass a pointer to a function that accepts a reference to an object. Can someone tell me how to do this?
    My code looks something like the following:

    Code:
    UserMatrix mat;
    UserMatrix *ptrMat;
    ptrMat = &mat;
    
    
    ......
    
    void setUpMat(UserMatrix &matrix)
    {
        matrix[0] = 1;
       ....
    }
    So how can I call the function setUpMatrix with ptrMat??

    Thanks for your help
    Mark

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Code:
    setUpMat ( *ptrMat );

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Why would you need a pointer, when you're already passing by reference.

    Given your current function, you would do
    setUpMat( mat );

    There's no need for the pointer.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am trying to pass a pointer to a function that accepts a reference to an object.
    You can't. C++ requires that the arguments you send to a function be the same type as the parameter type. A pointer type and a reference type are not the same types.

    As Slacker showed, you can dereference the pointer to get the object at that address and then send that object to the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. how to pass function pointer as arg in c++
    By umen242 in forum C++ Programming
    Replies: 13
    Last Post: 10-21-2008, 02:09 AM
  4. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21