Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Local var as reference to ptr

  1. Oct 8, 2005 #1
    i have this following code with local var as a "ref. to ptr"

    class node {
    CType* data;
    CTYpe* operator*() { return data; }
    void F(node *a, node *b)
    CType*& tp; tp=**a;

    I get the error "Cannot convert CType* to CType*&
    which i sort of understnad but i do not know how to resolve this.
    The reason i'm looking to do this is so that i don't have (**a).Data() throughout my code but rather tp.Data(); and i need it to be a reference otherwise it would not change the variable outside of hte Fx.

    I guess i can use a ptr to ptr(CType**) but i'd much rather just use a C++ reference. and if i do tp=***a i still get a error.

    thanks in advance
  2. jcsd
  3. Oct 8, 2005 #2
    The example is kind of hard to follow, what is it youre trying to do? could you spell out the goal of the coding?

    Using the Address of operator, is that for a function call? Do you want to manipulate objects with a pass by reference? It looks like a 3D array in there?
  4. Oct 9, 2005 #3


    User Avatar
    Staff Emeritus

    Brad, he is overloading the unary operator *. The purpose of this is so when he asks for the pointer of a node object, he gets the pointer of data instead.

    neurocomp2003, If you do this:

    Ctype* tp; tp=**a;

    then you would do this:


    The other option is to do something like this:

    CType*tp; tp=a->data;

    Then you would do this:


    This is of course assuming you make data public. Either that, or you make the function F a friend of node.
    Last edited: Oct 9, 2005
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Local var as reference to ptr
  1. Is 'var' weak type. (Replies: 3)

  2. Undefined Reference (Replies: 10)

  3. Call by reference (Replies: 11)