meaning of *& syntax

H

Hansen

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
 
R

Rolf Magnus

Hansen said:
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.
 
C

Clark S. Cox III

Hansen said:
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.
 
G

Grizlyk

Hansen said:
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").
 
C

Clark S. Cox III

Grizlyk said:
"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.
 
C

Clark S. Cox III

Grizlyk said:
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)
 
C

Craig Scott

I just came across the following syntax in a class declaration:
Close, it's a reference to a pointer.


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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top