meaning of *& syntax

Discussion in 'C++' started by Hansen, Feb 13, 2007.

  1. Hansen

    Hansen Guest

    Hi there,

    I just came across the following syntax in a class declaration:

    my_base*& base_;

    What does that mean? Is it a pointer to a reference? In my head * and & are
    opposites meaning that my_base*& == my_base ...
    But I have a feeling that I'm wrong ... :eek:)

    Best Regards
    Hansen
     
    Hansen, Feb 13, 2007
    #1
    1. Advertising

  2. Hansen

    Rolf Magnus Guest

    Hansen wrote:

    > Hi there,
    >
    > I just came across the following syntax in a class declaration:
    >
    > my_base*& base_;
    >
    > What does that mean? Is it a pointer to a reference?


    No. There are no such things as pointers to references. It's just the other
    way round. It's a reference to a pointer.

    > In my head * and & are opposites meaning that my_base*& == my_base ...


    my_base*& is a type. What you mean would be *&my_base.
     
    Rolf Magnus, Feb 13, 2007
    #2
    1. Advertising

  3. Hansen wrote:
    > Hi there,
    >
    > I just came across the following syntax in a class declaration:
    >
    > my_base*& base_;
    >
    > What does that mean? Is it a pointer to a reference?


    Close, it's a reference to a pointer.

    > In my head * and & are
    > opposites meaning that my_base*& == my_base ...
    > But I have a feeling that I'm wrong ... :eek:)


    Just like (int&) is a reference to an int, (int*&) is a reference to an
    int pointer.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Feb 13, 2007
    #3
  4. Hansen

    Grizlyk Guest

    Hansen wrote:
    >
    > I just came across the following syntax in a class declaration:
    >
    > my_base*& base_;
    >
    > What does that mean?


    "cdecl/cppdecl" can help you sometimes to resolve c-language expressions.

    Also find a table with "C++ precedens of operators": all operators have
    divided on groups by "priority level" and "associativity" ("left to right"
    or "right to left").


    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
     
    Grizlyk, Feb 13, 2007
    #4
  5. Grizlyk wrote:
    > Hansen wrote:
    >> I just came across the following syntax in a class declaration:
    >>
    >> my_base*& base_;
    >>
    >> What does that mean?

    >
    > "cdecl/cppdecl" can help you sometimes to resolve c-language expressions.
    >
    > Also find a table with "C++ precedens of operators": all operators have
    > divided on groups by "priority level" and "associativity" ("left to right"
    > or "right to left").


    This is not entirely true, some of the relationships between the
    operators cannot be expressed in a simple precedence table.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Feb 13, 2007
    #5
  6. Hansen

    Grizlyk Guest

    Clark S. Cox III wrote:
    >>
    >> Also find a table with "C++ precedens of operators": all operators have
    >> divided on groups by "priority level" and "associativity" ("left to
    >> right"
    >> or "right to left").

    >
    > This is not entirely true, some of the relationships between the
    > operators cannot be expressed in a simple precedence table.


    For example?

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
     
    Grizlyk, Feb 13, 2007
    #6
  7. Grizlyk wrote:

    >> This is not entirely true, some of the relationships between the
    >> operators cannot be expressed in a simple precedence table.

    > For example?


    Yes.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Feb 13, 2007
    #7
  8. Grizlyk wrote:
    > Clark S. Cox III wrote:
    >>> Also find a table with "C++ precedens of operators": all operators have
    >>> divided on groups by "priority level" and "associativity" ("left to
    >>> right"
    >>> or "right to left").

    >> This is not entirely true, some of the relationships between the
    >> operators cannot be expressed in a simple precedence table.

    >
    > For example?
    >


    Consider the relationship between the '=' operator and the '?:' operator:

    a?b:c=3; //Equivalent to a?b:(c=3)
    a?b=3:c; //Equivalent to a?(b=3):c
    a=3?b:c; //Equivalent to a=(3?b:c)


    --
    Clark S. Cox III
     
    Clark S. Cox III, Feb 13, 2007
    #8
  9. Hansen

    Craig Scott Guest

    > > I just came across the following syntax in a class declaration:
    >
    > > my_base*& base_;

    >
    > > What does that mean? Is it a pointer to a reference?

    >
    > Close, it's a reference to a pointer.
    >
    > > In my head * and & are
    > > opposites meaning that my_base*& == my_base ...
    > > But I have a feeling that I'm wrong ... :eek:)

    >
    > Just like (int&) is a reference to an int, (int*&) is a reference to an
    > int pointer.


    Since the OP hasn't seen this before, I mention the following as
    additional info. It can be useful to pass a "reference to a pointer"
    to functions that need to change what the pointer points to from the
    *caller's* point of view. For example (this is a very contrived
    example):

    void func(int*& foo, int* t)
    {
    // A real function would obviously do something
    // more meaningful here!
    foo = t;
    }

    int main(int argc, char* argv[])
    {
    int a = 0;
    int b = 1;
    int* myFoo = &a;

    // This prints the value of a, which is 0
    std::cout << *myFoo << std::endl;

    // This will change myFoo to point to b
    func(myFoo, &b);

    // This now prints the value of b, which is 1
    std::cout << *myFoo << std::endl;
    }


    Member variables can also be used in a similar fashion, but in my
    experience, that is less common (but not any more or less valid).

    --
    Computational Fluid Dynamics, CSIRO (CMIS)
    Melbourne, Australia
     
    Craig Scott, Feb 14, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    821
  2. gabriele renzi
    Replies:
    2
    Views:
    242
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    247
  4. Good Night Moon
    Replies:
    9
    Views:
    320
    Rick DeNatale
    Jul 25, 2007
  5. Jacob Grover
    Replies:
    5
    Views:
    352
    Jacob Grover
    Jul 18, 2008
Loading...

Share This Page