-4

As a brief background, since this question is about a specific type of memory management: I know "*" is a pointer when used as a postfix (i.e. after a type name) and can also be used as a dereferencing operator if it is used as a prefix, but I am confused about what the "&" equivalent is of that.

In other words, "&" is the address of operator as a prefix... but what does it mean as a postfix?

My question is how you would describe a postfix "&" (address of) operator. For instance, how would you describe something like:

vector<Shape∗>& v

in plain English?

  • 3
    In plain English I'd tell you to find a good beginner's book which explains references. Even plainer, google C++ references. – DeiDei Jun 02 '18 at 22:21
  • That seems like a reasonable answer, but for whatever reason, the books I've used focus on explaining the postfix and prefix uses of "*" and only the prefix usage of "&", even while some of them use the postfix version (i.e. use without explaining it). Basically, finding an answer doesn't seem to be quite that simple (though it should be...). – John Jenkins Jun 02 '18 at 22:23
  • There should be good books? Yes. You know very well the world is full of bad material. Cheers – DeiDei Jun 02 '18 at 22:24
  • Why is there a black block after Shape? – Yakk - Adam Nevraumont Jun 02 '18 at 22:25

1 Answers1

2

The ampersand in vector<Shape∗>& v is not an address-of operator, just like a star in int* p; is not a dereference operator. The former declares v to be of reference type; the latter declares p to be of pointer type

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Can you elaborate on that a bit? I understand that int* p is declaring p to be of pointer type. My understanding is that a pointer type literally is something that represents a pointer to an address. Is the reference type literally the address (i.e. not a pointer to one)? I'm guessing that based on the fact that & is the "address of" operator (as a prefix). – John Jenkins Jun 02 '18 at 22:27
  • Admittedly, just knowing that it is referred to as the reference type is very useful information. I may be able to figure out the rest just knowing that, but feel free to elaborate if you want. – John Jenkins Jun 02 '18 at 22:30
  • I trust you know how to follow links. The first sentence of the article I linked to reads, and I quote: "Declares a named variable as a reference, that is, *an alias to an already-existing object or function*." Emphasis mine. – Igor Tandetnik Jun 02 '18 at 22:31
  • Lol yeah perhaps my use of "may be able" misled you, but I meant I can figure it out from here, but feel free to elaborate. Of course you're under no obligation to do so. I'd upvote your response but I don't have enough reputation yet. – John Jenkins Jun 02 '18 at 22:56
  • @IgorTandetnik With all due respect, cppreference is not a good place to learn C++. As mentioned above, what the OP needs to do is [read a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders Jun 03 '18 at 22:22