Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reference (C++)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Relationship to pointers == C++ references differ from pointers in several essential ways: * A reference itself is not an object, it is an alias; any occurrence of its name refers directly to the object it references. A pointer declaration creates a pointer object which is distinct from the object the pointer refers to. ** Containers of references are not allowed because references are not objects, while containers of pointers are commonplace for polymorphism. ** It is not allowed to create a reference of reference, because references can only refer to objects (or functions). * References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In * Once a reference is created, it cannot be later made to reference another object; it cannot be ''reseated''. This is often done with pointers. * References cannot be ''null'', whereas pointers can; every reference refers to some object, although it may or may not be valid. particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor. For example: *: <syntaxhighlight lang="cpp"> int& k; // compiler will complain: error: `k' declared as reference but not initialized </syntaxhighlight> There is a simple conversion between pointers and references: the address-of operator (<code>&</code>) will yield a pointer referring to the same object when applied to a reference, and a reference which is initialized from the dereference (<code>*</code>) of a pointer value will refer to the same object as that pointer, where this is possible without invoking undefined behavior. This equivalence is a reflection of the typical implementation, which effectively compiles references into pointers which are implicitly dereferenced at each use. Though that is usually the case, the C++ Standard does not force compilers to implement references using pointers. A consequence of this is that in many implementations, operating on a variable with automatic or static lifetime through a reference, although syntactically similar to accessing it directly, can involve hidden dereference operations that are costly. Also, because the operations on references are so limited, they are much easier to understand than pointers and are more resistant to errors. While pointers can be made invalid through a variety of mechanisms, ranging from carrying a null value to out-of-bounds arithmetic to illegal casts to producing them from arbitrary integers, a previously valid reference only becomes invalid in two cases: * If it refers to an object with automatic allocation which goes out of scope, * If it refers to an object inside a block of dynamic memory which has been freed. The first is easy to detect automatically if the reference has static scoping, but is still a problem if the reference is a member of a dynamically allocated object; the second is more difficult to detect. These are the only concerns with references, and are suitably addressed by a reasonable allocation policy.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)