Reference (programming)

from Wikipedia, the free encyclopedia

A reference is a reference to an object . A reference is thus an alias name for an already existing object.

Definitions

Pointers are the most primitive form of reference. Because of their close relationship with the underlying hardware , they are one of the most powerful and efficient types of references. Because of this relationship, however, pointers require a strong understanding of the details of the memory architecture by the programmer . Because pointers store the memory address directly instead of a value, improper use of pointers can lead to undefined behavior in a program , particularly due to dangling pointers or placeholders. Smart pointers are opaque data structures that behave like pointers, but which can only be accessed using certain methods .

A handle is an abstract reference and can be represented in different ways. A common example are file handles that are used to abstract file contents. It usually represents both the file itself, as when obtaining a lock on the file, and a specific position within the file's contents, as when reading a file.

In distributed systems , the reference can contain more than one memory address or identifier . It can also include an embedded specification of the network protocols used to locate and access the referenced object and the way information is encoded or serialized . For example, a WSDL description of a remote web service can be viewed as a reference form. It contains a full specification on how to find and bind a particular web service . A reference to a live distributed object is another example: it's a complete specification for creating a small piece of software called a proxy and then performing a peer-to-peer interaction that the local computer may use to access receives data that is replicated or only exists as a weakly consistent message stream. In all of these cases, the reference contains the full set of instructions or a recipe for accessing the data. In this sense, it serves the same purpose as an identifier or a memory address.

References in C ++

In the C ++ programming language , references are used very frequently and for various purposes:

  1. as a (shorter or more understandable) alias name for an already existing object
  2. to optimize to avoid copies of objects
  3. in special member functions such as copy & move constructors and assignment operators
  4. as so-called universal reference (engl .: universal reference ) which, when Templates an arbitrary parameter type represented.

In C ++ there are so-called Lvalue references , which are identified by an appended to the type &, and (since C ++ 11 ) additional Rvalue references , which are &&identified by.

Code samples

Difference between reference and copy:

int original = 5;
int kopie = original;
int& referenz = original;

kopie    = 30;  // weist der Kopie den Wert 30 zu. Das Original bleibt unverändert
referenz = 20;  // weist der Referenz – und somit auch dem Original – den Wert 20 zu
original = 10;  // ändert das Original, womit aber auch die Referenz ihren Wert ändert.

Parameter transfer for reference :

void quadrieren(int& x) {
    x = x * x;
}

int main() {
    int i = 5;
    quadrieren(i);  // Funktionsaufruf ändert den Wert von i auf 25
}

Object references:

Bank& nBank = Bankenverzeichnis::nachBLZ("76543210");  // eine Referenz auf ein Bankobjekt wird beschafft
Konto& nKonto1 = nBank.kontoZugriff("1234567");        // eine Referenz auf ein bestimmtes Kontoobjekt wird beschafft
Konto& nKonto2 = nBank.kontoZugriff("1111111");        // eine Referenz auf ein weiteres Kontoobjekt wird beschafft
nKonto1.einzahlung(100.00, "EUR", nKonto2);            // eine Methode wird auf nKonto1 gerufen

Class design:

class Kunde
{
public:
    explicit Kunde(const std::string& name);  // 'name' wird aus Effizienzgründen nur als const-Referenz übergeben
    explicit Kunde(std::string&& name);       // Rvalue-Referenz, erlaubt ein "move" aus dem Namen (seit C++11)

    Kunde(const Kunde& other);  // Copy-Konstruktor
    Kunde(Kunde&& other);       // Move-Konstruktor (seit C++11)

    const std::string& getName() const;  // gibt const-Referenz auf Kundennamen zurück
    std::string&& getName() &&;  // gibt Rvalue-Referenz zurück, falls Objekt selbst ein RValue ist (ab C++11)

};

Example in Pascal

Transfer by reference (the value of the transferred variable is changed)

procedure quadriere(var wert: integer);
begin
    wert := wert * wert;
end;

Only a variable can be passed to this procedure , no expression. wertis the local name of the variable transferred as reference . With the assignment of a value of the content of the transferred variable is changed directly.

Transfer by value (i.e. only the value, not the variable itself; the value of the transferred variable is not changed)

function quadrat(wert: integer): integer;
begin
    Result := wert * wert;
end;

Even if an allocation of wertwould take place, this would the contents of a gathered from any variables not change: Pass only one value . The identifier wert stands for a local variable that is only valid within the function .