C++ Funktion - Returntyp Array

  • C++

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von Radinator.

    C++ Funktion - Returntyp Array

    Hoi @all!

    Doofe Frage, aber wie genau gibt man in C++ ein Array zurück? In VB geht das ja mit

    VB.NET-Quellcode

    1. Private Function test() As Integer()
    2. Dim i As Integer()
    3. Return i
    4. End Function

    in C#

    C#-Quellcode

    1. int[] Test()
    2. {
    3. int[] i = new int[5];
    4. return i;
    5. }

    gemacht. Wenn ich aber in C++

    C-Quellcode

    1. int[] Test(long){
    2. }

    Dann wird mir Test unterringelt und angemerkt es fehle ein ';'. Was mach ich falsch?

    Hab schon ein paar Seiten besucht, einige deklarieren einfach

    C-Quellcode

    1. int* Test(long){
    2. }

    und returnen eine Referenz auf ein Array, dass sie intern/lokal anlegen. Kann mir aber nich vorstellen, dass das so gewollt ist.

    Wäre suuuper wenn wer ne Antwort weiß


    Lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    int* ist keine Referenz in diesem Sinn, eine Referenz wäre int&, int* hingegen ist ein pointer, welcher auf eine Speicherstelle zeigt.
    Erstmal die Frage, bist du dir sicher, dass Array der gewollte Typ ist?
    Ich wollte auch mal ne total überflüssige Signatur:
    ---Leer---
    Das geht in C++ nicht. Du kannst nen Pointer zurück geben nur musst du dann wissen wie viele Elemente da zurück kommen. Entweder ist das fix oder du gibst das zusätzlich zurück.
    Oder du nimmst z.B. std::array als Rückgabetyp. Müsste C++11 und aufwärts verfügbar sein.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    Mach es am Besten wie Gonger sagte mit einem vector<>. Wende in dem Fall aber Move Semantics an. return move(...);.
    Alternativ wie thefiloe gesagt hat ginge das noch mit 'nem Pointer und dann inkrementierst Du den. Brauchst dann dafür eben diesen Count, damit Du die Begrenzung hast, um nicht in fremden Speicher zu lesen und eine Access Violation auszulösen.
    Solche Pointer, mit denen Du Speicher allokierst, müssen natürlich freigegeben werden.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Ok danke an alle
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell