<?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[operator überladen]]></title><description><![CDATA[<p>Hi ich hab ne klasse die ungefähr so aussieht:</p>
<pre><code class="language-cpp">class Surface
{
public:
	Surface(int width, int height);
	~Surface();

	Color &amp;operator[] (int index)
	{
		return pixels[index];
	}

	Color &amp;getPixel(int x, int y)
	{
		return pixels[/*pos*/];
	}

private:
	Color *pixels;
};
</code></pre>
<p>Da ich den speicher zusammenhängend haben will möchte ich nur ein ein-dimensionales array benutzen, würde aber trozdem gerne über [][] (anstadt getPixel) drauf zugreifen können, gibts da eine möglichkeit?</p>
<p>Gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/208730/operator-überladen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 21:33:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/208730.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 21 Mar 2008 23:30:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to operator überladen on Fri, 21 Mar 2008 23:30:17 GMT]]></title><description><![CDATA[<p>Hi ich hab ne klasse die ungefähr so aussieht:</p>
<pre><code class="language-cpp">class Surface
{
public:
	Surface(int width, int height);
	~Surface();

	Color &amp;operator[] (int index)
	{
		return pixels[index];
	}

	Color &amp;getPixel(int x, int y)
	{
		return pixels[/*pos*/];
	}

private:
	Color *pixels;
};
</code></pre>
<p>Da ich den speicher zusammenhängend haben will möchte ich nur ein ein-dimensionales array benutzen, würde aber trozdem gerne über [][] (anstadt getPixel) drauf zugreifen können, gibts da eine möglichkeit?</p>
<p>Gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1478558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1478558</guid><dc:creator><![CDATA[obololo]]></dc:creator><pubDate>Fri, 21 Mar 2008 23:30:17 GMT</pubDate></item><item><title><![CDATA[Reply to operator überladen on Sat, 22 Mar 2008 10:07:22 GMT]]></title><description><![CDATA[<p>Dazu brauchst du eine zweite Helfer Klasse:</p>
<pre><code class="language-cpp">class HelpColor {

public:
HelpColor(int _index, Color *_pixels) : index(_index), pixels(_pixels) { }

Color &amp;operator[] (int y) {
    return pixels[index*y];
}

private:
int index;
Color *pixels;
};

class Surface
{
public:
Surface(int width, int height);
~Surface();

HelpColor operator[] (int index)
{
return HelpColor(index,pixels);
}

Color &amp;getPixel(int x, int y)
{
return pixels[x*y];
}

private:
Color *pixels;
};
</code></pre>
<p>Ungetestet</p>
<p>//EDIT<br />
Irgendwas stimmt wohl mit den Code Tags nicht wenn man mit dem Operator [] hantiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1478611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1478611</guid><dc:creator><![CDATA[Mario Sandler]]></dc:creator><pubDate>Sat, 22 Mar 2008 10:07:22 GMT</pubDate></item><item><title><![CDATA[Reply to operator überladen on Sat, 22 Mar 2008 10:23:54 GMT]]></title><description><![CDATA[<p><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>Dazu brauchst du eine zweite Helfer Klasse:</p>
</blockquote>
<p>Im Allgemeinen benötigen wir ein Proxy-Objekt. In unserem speziellen Fall genügt auch ein einfacher Zeiger:</p>
<pre><code class="language-cpp">class Surface
{
public:
    ...

    Color* operator[](int x)
    {
        return pixels+x*dim_y;
    }

    Color&amp; getPixel(int x, int y)
    {
        return pixels[x*dim_y+y];
    }

private:
    Color* pixels;
    int dim_x;
    int dim_y;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1478616</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1478616</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 22 Mar 2008 10:23:54 GMT</pubDate></item><item><title><![CDATA[Reply to operator überladen on Sat, 22 Mar 2008 14:12:09 GMT]]></title><description><![CDATA[<p>Super, thx!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1478735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1478735</guid><dc:creator><![CDATA[bolololo]]></dc:creator><pubDate>Sat, 22 Mar 2008 14:12:09 GMT</pubDate></item></channel></rss>