<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Hilfestellung bei der Operatorüberladung]]></title><description><![CDATA[<p>Hey Leute,</p>
<p>Habe heute mal Operatorüberladung gemacht und soweit funktioniert das auch alles. Nun frage ich mich wie man dafür sorgen kann, dass folgendes in der Main geschrieben werden kann:</p>
<pre><code>test[1] = 7;
</code></pre>
<pre><code>#include &lt;iostream&gt;

class Element{
public:
    Element *next;
    int value;

    Element(int inputValue){
        this-&gt;value = inputValue;
        this-&gt;next = nullptr;
    }
};

class Liste{
public:
    Element *start;
    Element *last;

    Liste(int val = 0){
        start = 0;
        last = 0;
        int i;
        for(i=0; i&lt;val; i++){
            push(0);
        }
    }
    ~Liste(){
        if(start != nullptr &amp;&amp; last != nullptr){
            Element *temp = 0;
            while(start != nullptr){
                temp = start;
                start = temp-&gt;next;
                delete temp;
            }
        }
    }
    void push(int a){
        Element *temp = new Element(a);
        if(start == 0){
            start = temp;
            last = temp;
        } else{
            last-&gt;next = temp;
            last = temp;
        }
    }
    void rep(int position, int reValue){
        int i;
        Element *pos = start;
        for(i=0;i &lt; position; i++){
            if(pos == nullptr){
                std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot;;
            }else{
                pos = pos-&gt;next;
            }
        }
        if(pos == nullptr){
                std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot; &lt;&lt; std::endl;
                return;
            }
        pos-&gt;value = reValue;
    }
    int getAt(int position){
        int i;
        Element *pos = start;
        for(i=0; pos != last &amp;&amp; i &lt; position; i++){
            pos = pos-&gt;next;
        }
        if(pos == 0){
            std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot; &lt;&lt; std::endl;
            exit;
        }
        return pos-&gt;value;
    }
    int operator[](const int &amp;x){
        return getAt(x);
    }
};

int main(){
    int i;
    Liste test(3);
    for(i=0; i&lt;3; i++){
        std::cout &lt;&lt; test.getAt(i) &lt;&lt; std::endl;
    }
    test.rep(0,3);
    test.rep(1,2);
    test.rep(2,1);

    for(i=0; i&lt;3; i++){
        std::cout &lt;&lt; test.getAt(i) &lt;&lt; std::endl;
    }

    test.push(5);

    for(i=0; i&lt;4; i++){
        std::cout &lt;&lt; test[i] &lt;&lt; std::endl;
    }

    return 0;
}
</code></pre>
<p>Schonmal Danke für eure Hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/334008/hilfestellung-bei-der-operatorüberladung</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 05:28:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334008.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 16 Aug 2015 11:41:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hilfestellung bei der Operatorüberladung on Sun, 16 Aug 2015 11:41:09 GMT]]></title><description><![CDATA[<p>Hey Leute,</p>
<p>Habe heute mal Operatorüberladung gemacht und soweit funktioniert das auch alles. Nun frage ich mich wie man dafür sorgen kann, dass folgendes in der Main geschrieben werden kann:</p>
<pre><code>test[1] = 7;
</code></pre>
<pre><code>#include &lt;iostream&gt;

class Element{
public:
    Element *next;
    int value;

    Element(int inputValue){
        this-&gt;value = inputValue;
        this-&gt;next = nullptr;
    }
};

class Liste{
public:
    Element *start;
    Element *last;

    Liste(int val = 0){
        start = 0;
        last = 0;
        int i;
        for(i=0; i&lt;val; i++){
            push(0);
        }
    }
    ~Liste(){
        if(start != nullptr &amp;&amp; last != nullptr){
            Element *temp = 0;
            while(start != nullptr){
                temp = start;
                start = temp-&gt;next;
                delete temp;
            }
        }
    }
    void push(int a){
        Element *temp = new Element(a);
        if(start == 0){
            start = temp;
            last = temp;
        } else{
            last-&gt;next = temp;
            last = temp;
        }
    }
    void rep(int position, int reValue){
        int i;
        Element *pos = start;
        for(i=0;i &lt; position; i++){
            if(pos == nullptr){
                std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot;;
            }else{
                pos = pos-&gt;next;
            }
        }
        if(pos == nullptr){
                std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot; &lt;&lt; std::endl;
                return;
            }
        pos-&gt;value = reValue;
    }
    int getAt(int position){
        int i;
        Element *pos = start;
        for(i=0; pos != last &amp;&amp; i &lt; position; i++){
            pos = pos-&gt;next;
        }
        if(pos == 0){
            std::cout &lt;&lt; &quot;OUT OF BOUNDS&quot; &lt;&lt; std::endl;
            exit;
        }
        return pos-&gt;value;
    }
    int operator[](const int &amp;x){
        return getAt(x);
    }
};

int main(){
    int i;
    Liste test(3);
    for(i=0; i&lt;3; i++){
        std::cout &lt;&lt; test.getAt(i) &lt;&lt; std::endl;
    }
    test.rep(0,3);
    test.rep(1,2);
    test.rep(2,1);

    for(i=0; i&lt;3; i++){
        std::cout &lt;&lt; test.getAt(i) &lt;&lt; std::endl;
    }

    test.push(5);

    for(i=0; i&lt;4; i++){
        std::cout &lt;&lt; test[i] &lt;&lt; std::endl;
    }

    return 0;
}
</code></pre>
<p>Schonmal Danke für eure Hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2464075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2464075</guid><dc:creator><![CDATA[Abling1981]]></dc:creator><pubDate>Sun, 16 Aug 2015 11:41:09 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfestellung bei der Operatorüberladung on Sun, 16 Aug 2015 11:56:00 GMT]]></title><description><![CDATA[<p>Dazu muss dein <code>operator[]</code> eine Referenz zurück geben. Und als Parameter hätte ich auch einen normalen int genommen statt const reference, deine getAt Funktion hat ja auch int als Parameter. Wenn du es so schreibst sollteste es gehen:</p>
<pre><code>int&amp; getAt(int position)
int&amp; operator[](int x)

// Eventuell zusätzlich noch die const Variante davon
const int&amp; getAt(int position) const
const int&amp; operator[](int x) const
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2464081</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2464081</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sun, 16 Aug 2015 11:56:00 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfestellung bei der Operatorüberladung on Sun, 16 Aug 2015 12:19:09 GMT]]></title><description><![CDATA[<p>aah, danke.</p>
<p>Auf die Lösung hätte ich selber kommen müssen XD</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2464086</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2464086</guid><dc:creator><![CDATA[Abling1981]]></dc:creator><pubDate>Sun, 16 Aug 2015 12:19:09 GMT</pubDate></item></channel></rss>