<?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[Probleme mit Sichtbarkeitsmodifikatoren]]></title><description><![CDATA[<p>Hallo, ich möchte eine Vector Klasse schreiben.<br />
Damit diese schnell angewendet werden kann definiere ich die Funktionen als inline.</p>
<p>Dazu habe ich folgende Klasse:</p>
<pre><code class="language-cpp">class Vector3D
{
public:
	float x,y,z;
public:
	Vector3D(float x_,float y_,float z_): x(x_), y(y_), z(z_){};
	Vector3D():x(0),y(0),z(0) {};

	~Vector3D(void){}
...
};
inline Vector3D operator + (const Vector3D&amp; a,const Vector3D&amp; b)	{return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);}
</code></pre>
<p>Den + Operator mußte ich außerhalb der Klasse definieren, weil sonst der error kommt, dass + zu viele Pararmeter hat.</p>
<blockquote>
<p>error C2804: binary 'operator +' has too many parameters</p>
</blockquote>
<p>Wenn ich nun die Variablen</p>
<pre><code class="language-cpp">float x,y,z;
</code></pre>
<p>als Private setze kennt die außerhalb definierte Funktion für + diese nicht mehr.</p>
<p>Wie kann ich die Variablen schützen (den Zugriff einschränken), so dass ich trotzdem noch den + Operator für 2 Pararmeter und als inline definieren kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/190333/probleme-mit-sichtbarkeitsmodifikatoren</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 14:59:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/190333.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 21 Aug 2007 16:53:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 16:53:49 GMT]]></title><description><![CDATA[<p>Hallo, ich möchte eine Vector Klasse schreiben.<br />
Damit diese schnell angewendet werden kann definiere ich die Funktionen als inline.</p>
<p>Dazu habe ich folgende Klasse:</p>
<pre><code class="language-cpp">class Vector3D
{
public:
	float x,y,z;
public:
	Vector3D(float x_,float y_,float z_): x(x_), y(y_), z(z_){};
	Vector3D():x(0),y(0),z(0) {};

	~Vector3D(void){}
...
};
inline Vector3D operator + (const Vector3D&amp; a,const Vector3D&amp; b)	{return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);}
</code></pre>
<p>Den + Operator mußte ich außerhalb der Klasse definieren, weil sonst der error kommt, dass + zu viele Pararmeter hat.</p>
<blockquote>
<p>error C2804: binary 'operator +' has too many parameters</p>
</blockquote>
<p>Wenn ich nun die Variablen</p>
<pre><code class="language-cpp">float x,y,z;
</code></pre>
<p>als Private setze kennt die außerhalb definierte Funktion für + diese nicht mehr.</p>
<p>Wie kann ich die Variablen schützen (den Zugriff einschränken), so dass ich trotzdem noch den + Operator für 2 Pararmeter und als inline definieren kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349205</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349205</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Tue, 21 Aug 2007 16:53:49 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 17:04:04 GMT]]></title><description><![CDATA[<p>Was du suchst ist das Schlüsselwort <strong>friend</strong>.</p>
<p>Wenn du eine schnelle Vektor-Klasse willst, solltest du SIMD verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349208</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Tue, 21 Aug 2007 17:04:04 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 17:32:45 GMT]]></title><description><![CDATA[<p>Ist es sinnvoll die Methoden get_x() get_y() und get_z() einzubauen oder sollte man die member einfach public lassen und keine get und set Methoden verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349221</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349221</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Tue, 21 Aug 2007 17:32:45 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 17:41:36 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>Ist es sinnvoll die Methoden get_x() get_y() und get_z() einzubauen oder sollte man die member einfach public lassen und keine get und set Methoden verwenden?</p>
</blockquote>
<p>Die Klasse muss ihre Invariante sicherstellen können, auf deutsch gesagt, sie muss sicherstellen, dass niemand ihre Daten so durcheinanderwürfelt, dass die Klasse selbst damit nichts mehr anfangen kann. Daraus folgert man normalerweise, dass der Zugriff auf sämtliche Membervariablen geschützt wird, d.h. nur durch eine Auswahl von Methoden möglich ist.<br />
Wenn es aber, wie hier, keine Invariante zu schützen gibt, weil schlicht und einfach jede mögliche Kombination von x, y, z auch gültig ist, kann man Felder IMHO auch public machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349227</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349227</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Tue, 21 Aug 2007 17:41:36 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 22:01:01 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<pre><code class="language-cpp">inline Vector3D operator + (const Vector3D&amp; a,const Vector3D&amp; b)	
{return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);}
</code></pre>
</blockquote>
<p>Mach das mal so :...</p>
<pre><code class="language-cpp">inline Vector3D operator + (const Vector3D&amp; a)const	
{return Vector3D(a.x + x, a.y + y, a.z + z);}
</code></pre>
<p>Dann brauchst du auch nicht die friend deklaration.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349377</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349377</guid><dc:creator><![CDATA[megaweber]]></dc:creator><pubDate>Tue, 21 Aug 2007 22:01:01 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 22:02:09 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>Die Klasse muss ihre Invariante sicherstellen können, auf deutsch gesagt, sie muss sicherstellen, dass niemand ihre Daten so durcheinanderwürfelt, dass die Klasse selbst damit nichts mehr anfangen kann. Daraus folgert man normalerweise, dass der Zugriff auf sämtliche Membervariablen geschützt wird, d.h. nur durch eine Auswahl von Methoden möglich ist.<br />
Wenn es aber, wie hier, keine Invariante zu schützen gibt, weil schlicht und einfach jede mögliche Kombination von x, y, z auch gültig ist, kann man Felder IMHO auch public machen.</p>
</blockquote>
<p>Oder man definiert einen [] operator. Finde ich auch sehr praktisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349382</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349382</guid><dc:creator><![CDATA[megaweber]]></dc:creator><pubDate>Tue, 21 Aug 2007 22:02:09 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Tue, 21 Aug 2007 22:32:56 GMT]]></title><description><![CDATA[<p>megaweber schrieb:</p>
<blockquote>
<p>Mach das mal so :...</p>
<pre><code class="language-cpp">inline Vector3D operator + (const Vector3D&amp; a)const	
{return Vector3D(a.x + x, a.y + y, a.z + z);}
</code></pre>
</blockquote>
<p>Oder mach operator+= als Member und operator+ als freie Funktion, die operator+= aufruft. Dann braucht man auch keine friend-Deklaration.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349389</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Tue, 21 Aug 2007 22:32:56 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Wed, 22 Aug 2007 10:09:49 GMT]]></title><description><![CDATA[<p>Hallo, ich habe mich jetzt entschieden kein set und get Methoden zu verwenden, da es ja wirklich keine zu schützende Invariante gibt.</p>
<pre><code class="language-cpp">#include &lt;math.h&gt;

#pragma once

class Vector3D
{
public:
	float x,y,z;
public:
	Vector3D(const float x_,const float y_,const float z_): x(x_), y(y_), z(z_){};

	Vector3D():x(0),y(0),z(0) {};

	Vector3D(const Vector3D&amp; v): x(v.x), y(v.y), z(v.z) {} 

	~Vector3D(void){}

	inline Vector3D get_Vector() {return Vector3D(x,y,z);}

	inline void set_Vector(const float x_,const float y_,const float z_) {x=x_; y=y_; z=z_;}

    inline Vector3D&amp; operator =  (const Vector3D&amp; v)	{x = v.x; y = v.y; z = v.z; return *this;}
	inline Vector3D&amp; operator += (const Vector3D&amp; v)	{x += v.x; y += v.y; z += v.z; return *this;}
	inline Vector3D&amp; operator -= (const Vector3D&amp; v)	{x -= v.x; y -= v.y; z -= v.z; return *this;}
	inline Vector3D&amp; operator *= (const Vector3D&amp; v)	{x *= v.x; y *= v.y; z *= v.z; return *this;}
	inline Vector3D&amp; operator *= (const float f)		{x *= f; y *= f; z *= f; return *this;}
	inline Vector3D&amp; operator /= (const Vector3D&amp; v)	{if ((v.x==0) || (v.y==0) || (v.z==0)){throw &quot;Division duch 0 Klasse Vector3D Operator /= Vector3D&quot;;}x /= v.x; y /= v.y; z /= v.z; return *this;}
	inline Vector3D&amp; operator /= (const float f)		{if (f==0){throw &quot;Division duch 0 Klasse Vector3D Operator /= float&quot;;}x /= f; y /= f; z /= f; return *this;}

	inline Vector3D kreuz(const Vector3D b) {return Vector3D(y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x);}

	inline void Vector3D::normalize() {float laenge_Kehrwert =1 / Vector3D::length(); x*=laenge_Kehrwert; y*=laenge_Kehrwert; z*=laenge_Kehrwert;}

    inline float Vector3D::length() {return sqrt(x*x+y*y+z*z);}

    inline float Vector3D::sqrt_length() {return (x*x+y*y+z*z);}

};

	 inline bool operator == (const Vector3D&amp; a, const Vector3D&amp; b) {if(a.x != b.x) return false; if(a.y != b.y) return false; return a.z == b.z;}
     inline bool operator != (const Vector3D&amp; a, const Vector3D&amp; b) {if(a.x != b.x) return true; if(a.y != b.y) return true; return a.z != b.z;}

     inline Vector3D operator + (const Vector3D&amp; a,const Vector3D&amp; b)	{return Vector3D(a.x + b.x, a.y + b.y, a.z + b.z);}
   	 inline Vector3D operator - (const Vector3D&amp; a,const Vector3D&amp; b)	{return Vector3D(a.x - b.x, a.y - b.y, a.z - b.z);}
     inline Vector3D operator - (const Vector3D&amp; a)					    {return Vector3D(-a.x, -a.y, -a.z );}
	 inline Vector3D operator * (const Vector3D&amp; a, const Vector3D&amp; b)	{return Vector3D(a.x * b.x, a.y * b.y, a.z * b.z);}
	 inline Vector3D operator * (const Vector3D&amp; a, const float f)		{return Vector3D(a.x * f, a.y * f, a.z * f);}
	 inline Vector3D operator * (const float f, const Vector3D&amp; a)		{return Vector3D(a.x * f, a.y * f, a.z * f);}
	 inline Vector3D operator / (const Vector3D&amp; a, const Vector3D&amp; b)	{if ((b.x==0) || (b.y==0) || (b.z==0)){throw &quot;Division duch 0 Klasse Vector3D Operator /&quot;;}return Vector3D(a.x / b.x, a.y / b.y, a.z / b.z);}
	 inline Vector3D operator / (const Vector3D&amp; a, const float f)		{if (f==0){throw &quot;Division duch 0 Klasse Vector3D Operator /&quot;;}float f_Kehrwert=1/f; return Vector3D(a.x * f_Kehrwert, a.y * f_Kehrwert, a.z * f_Kehrwert);}

	inline float skalar_Produkt(const Vector3D v1,const Vector3D v2)
	{
		return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;
	}

	//Kreuzprodukt
	 inline	Vector3D    kreuz(const Vector3D a,const Vector3D b) {return Vector3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);}

	 //berechnet den Winkel in 360 Grad
	 inline float  winkel(Vector3D vec1, Vector3D vec2) 
	 {
		Vector3D help = vec1*vec2; 
		float help1 = help.x+help.y+help.z;
		float help2 = vec1.length()+vec2.length();
		float help3 = help1/help2;
		return acos(help3)/0.017453292519943295769f;
	 }
</code></pre>
<p>Ist an dieser Vectorklasse noch irgend was falsch, oder verbesserungsfähig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349671</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 22 Aug 2007 10:09:49 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Wed, 22 Aug 2007 10:44:21 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>Ist an dieser Vectorklasse noch irgend was falsch, oder verbesserungsfähig?</p>
</blockquote>
<p>Den Deutsch/Englisch-Mix würde ich als verbesserungsfähig bezeichnen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349686</guid><dc:creator><![CDATA[*******]]></dc:creator><pubDate>Wed, 22 Aug 2007 10:44:21 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Wed, 22 Aug 2007 10:45:41 GMT]]></title><description><![CDATA[<p>Und die fehlende const-correctness.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349687</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349687</guid><dc:creator><![CDATA[*******]]></dc:creator><pubDate>Wed, 22 Aug 2007 10:45:41 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Wed, 22 Aug 2007 11:20:04 GMT]]></title><description><![CDATA[<p>******* schrieb:</p>
<blockquote>
<p>MisterX schrieb:</p>
<blockquote>
<p>Ist an dieser Vectorklasse noch irgend was falsch, oder verbesserungsfähig?</p>
</blockquote>
<p>Den Deutsch/Englisch-Mix würde ich als verbesserungsfähig bezeichnen.</p>
</blockquote>
<p>Stimmt: Habe ich alle auf englisch geändert</p>
<p>******* schrieb:</p>
<blockquote>
<p>Und die fehlende const-correctness.</p>
</blockquote>
<p>Wo genau habe ich const falsch oder zu wenig benutzt?<br />
(Ich kenn mich noch nicht so damit aus)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349710</guid><dc:creator><![CDATA[MisterX]]></dc:creator><pubDate>Wed, 22 Aug 2007 11:20:04 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Sichtbarkeitsmodifikatoren on Wed, 22 Aug 2007 11:27:48 GMT]]></title><description><![CDATA[<p>MisterX schrieb:</p>
<blockquote>
<p>******* schrieb:</p>
<blockquote>
<p>Und die fehlende const-correctness.</p>
</blockquote>
<p>Wo genau habe ich const falsch oder zu wenig benutzt?<br />
(Ich kenn mich noch nicht so damit aus)</p>
</blockquote>
<p>get_Vector() ist unnötigt (dafür hast du den Copy-Ctor), length() und sqrt_length() kannst du const setzen - und für das Kreuzprodukt reicht auch eine Version.</p>
<p>außerdem solltest du die globalen Operatoren durch die entsprechenden kombinierten Zuweisungen ausdrücken:</p>
<pre><code class="language-cpp">inline Vector3D operator+(const Vector3D&amp; l, const Vector3D&amp; r)
{
  return Vector3D(l)+=r;
}
</code></pre>
<p>PS: Und vernünftige Vektoren im mathematischen Sinn haben keine komponentenweise Multiplikation, sondern nur Skalar- und Kreuzprodukt (letzteres nur für 3D).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1349714</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1349714</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 22 Aug 2007 11:27:48 GMT</pubDate></item></channel></rss>