<?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[eingebettete Objekte]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich frickel an einem Anfaengerbeispiel rum, d.h. eine kleine, einfache Klassenhierarchie fuer geometrische Objekte - Punkte, Linien und was die Zukunft noch bringt...</p>
<p>Problem:<br />
Ich habe eine Basisklasse erstellt und meine erste abgeleitete, d.h. Punktklasse, die verschiedene Methoden umfasst (verschieben etc...). Auch eine polymorphe Report-Funktion hab ich dabei <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
Naechster Schritt waere nun eine Linie. Um's einfach zu machen hab ich's erstmal mit einer Gerade versucht. Leider krieg ichs nicht gebacken. Ein aehnlich gelagerter Frage-Antwort-Thread auf stackoverflow hat mir nicht geholfen. Kann mich jemand evtl. auf die richtige Bahn schubsen? Unten ist der Code. Methoden hab ich der Kuerze halber weggelassen.</p>
<p>Danke, Torsten!</p>
<pre><code class="language-cpp">// ----------------------------------- GeoObj.h --
#ifndef _GEO_OBJEKT_H
#define _GEO_OBJEKT_H
#include &lt;string&gt;

using namespace std;
class GeoObj
{
private:
  std::string name;  // objectname
  long int id;  // object-ID
public:
  // constr
  GeoObj(std::string newname, long int idx);
  // destr
  ~GeoObj();
};
#endif //_GEO_OBJEKT_H

// ------------------------------------ GeoObj.cpp --
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// base class for my simple Geo-objects

// constr
GeoObj::GeoObj(std::string newname, long int idx)
  {
    name = newname;
    id = idx;
    std::cout &lt;&lt; &quot;Create BasObj(&quot; &lt;&lt; name &lt;&lt; &quot;) -&gt; ID: &quot; &lt;&lt; id &lt;&lt; &quot;\n&quot;;
  }
// destr
GeoObj::~GeoObj()
  {
    std::cout &lt;&lt; &quot;from BaseObjs.\n&quot;;
  }

// ------------------------------------ Pt_2d.h --
#ifndef _PT_2D_H
#define _PT_2D_H
#include &quot;GeoObj.h&quot;
#include &lt;string&gt;

class Pt_2d : public GeoObj
{
private:
  double x, y;  // Position
public:
  // constr
  Pt_2d(std::string newname, double newx, double newy, long int idx);
  // destr
  ~Pt_2d();
};
#endif /*_PT_2D_H*/

// ------------------------------------ Pt_2d.cpp --
#include &quot;Pt_2d.h&quot;
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// constr
Pt_2d::Pt_2d(std::string newname, double newx, double newy, long int idx)
             : GeoObj(newname, idx)
  {
    x = newx;
    y = newy;
    std::cout &lt;&lt; &quot;Create Feature Point(&quot; &lt;&lt; x &lt;&lt; &quot;,&quot; &lt;&lt; y &lt;&lt; &quot;)\n&quot;;
  }
// desctr
Pt_2d::~Pt_2d()
  {
    std::cout &lt;&lt; &quot;Drop Point &quot;;
  }

// ------------------------------------ Line_2d.h --
#ifndef LINE_2D_H_
#define LINE_2D_H_
#include &quot;GeoObj.h&quot;
#include &quot;Pt_2d.h&quot;
#include &lt;string&gt;

class Line_2d : public GeoObj
{
private:
  Pt_2d *pt1;
  Pt_2d *pt2;

public:
// constr
  Line_2d(std::string newname, Pt_2d &amp;pt1, Pt_2d &amp;pt2, long int idx);
  // destr
  ~Line_2d();

};
#endif /*LINE_2D_H_*/

// ------------------------------------ Line_2d.cpp --
#include &quot;Pt_2d.h&quot;
#include &quot;Line_2d.h&quot;
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// constr
Line_2d::Line_2d(std::string newname, Pt_2d::Pt_2d newp1, Pt_2d::Pt_2d newp2, long int idx)
             : Pt_2d(newp1, idx)
  {
//    pt1 = newp1;
//    pt2 = newp2;
//    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;)\n&quot;;
  }
// destr
Line_2d::~Line_2d()
  {
    std::cout &lt;&lt; &quot;Drop Line &quot;;
  }

// ------------------------------------ main.cpp --
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &quot;Pt_2d.h&quot; 
#include &quot;Line_2d.h&quot; 
#include &quot;GeoObj.h&quot;
#include &quot;reports.h&quot;

using namespace std;
int main()
{
  cout &lt;&lt; &quot;\n\n&quot; &lt;&lt; &quot;First OOP...&quot; &lt;&lt; &quot;\n\n&quot;;
  // create two pts and one line from 2 pts
  Pt_2d p1(&quot;Punkt1&quot;, 0, 4, 0);
  Pt_2d p2(&quot;Punkt2&quot;, 1, 1, 1);
  Line_2d l1(&quot;Line1&quot;,p1,p2,2);
  // Verweise auf Objekte in Container speichern
  vector&lt;GeoObj*&gt; GeoBaseObj(0);
  GeoBaseObj.push_back( &amp;p1 );
  GeoBaseObj.push_back( &amp;p2 );
  GeoBaseObj.push_back( &amp;l1 );

  return 0;
}

// ------------------------------------ The make file:
makefile:
all: hello

clean:
        -rm main.o Pt_2d.o Line_2d.o GeoObj.o reports.o hello

hello: main.o
        g++ -g -o hello main.o Pt_2d.o Line_2d.o GeoObj.o reports.o

main.o: main.cpp
        g++ -c -g main.cpp Pt_2d.cpp Line_2d.cpp GeoObj.cpp reports.cpp
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/281789/eingebettete-objekte</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 10:34:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/281789.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 07 Feb 2011 22:19:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to eingebettete Objekte on Mon, 07 Feb 2011 22:19:58 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich frickel an einem Anfaengerbeispiel rum, d.h. eine kleine, einfache Klassenhierarchie fuer geometrische Objekte - Punkte, Linien und was die Zukunft noch bringt...</p>
<p>Problem:<br />
Ich habe eine Basisklasse erstellt und meine erste abgeleitete, d.h. Punktklasse, die verschiedene Methoden umfasst (verschieben etc...). Auch eine polymorphe Report-Funktion hab ich dabei <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
Naechster Schritt waere nun eine Linie. Um's einfach zu machen hab ich's erstmal mit einer Gerade versucht. Leider krieg ichs nicht gebacken. Ein aehnlich gelagerter Frage-Antwort-Thread auf stackoverflow hat mir nicht geholfen. Kann mich jemand evtl. auf die richtige Bahn schubsen? Unten ist der Code. Methoden hab ich der Kuerze halber weggelassen.</p>
<p>Danke, Torsten!</p>
<pre><code class="language-cpp">// ----------------------------------- GeoObj.h --
#ifndef _GEO_OBJEKT_H
#define _GEO_OBJEKT_H
#include &lt;string&gt;

using namespace std;
class GeoObj
{
private:
  std::string name;  // objectname
  long int id;  // object-ID
public:
  // constr
  GeoObj(std::string newname, long int idx);
  // destr
  ~GeoObj();
};
#endif //_GEO_OBJEKT_H

// ------------------------------------ GeoObj.cpp --
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// base class for my simple Geo-objects

// constr
GeoObj::GeoObj(std::string newname, long int idx)
  {
    name = newname;
    id = idx;
    std::cout &lt;&lt; &quot;Create BasObj(&quot; &lt;&lt; name &lt;&lt; &quot;) -&gt; ID: &quot; &lt;&lt; id &lt;&lt; &quot;\n&quot;;
  }
// destr
GeoObj::~GeoObj()
  {
    std::cout &lt;&lt; &quot;from BaseObjs.\n&quot;;
  }

// ------------------------------------ Pt_2d.h --
#ifndef _PT_2D_H
#define _PT_2D_H
#include &quot;GeoObj.h&quot;
#include &lt;string&gt;

class Pt_2d : public GeoObj
{
private:
  double x, y;  // Position
public:
  // constr
  Pt_2d(std::string newname, double newx, double newy, long int idx);
  // destr
  ~Pt_2d();
};
#endif /*_PT_2D_H*/

// ------------------------------------ Pt_2d.cpp --
#include &quot;Pt_2d.h&quot;
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// constr
Pt_2d::Pt_2d(std::string newname, double newx, double newy, long int idx)
             : GeoObj(newname, idx)
  {
    x = newx;
    y = newy;
    std::cout &lt;&lt; &quot;Create Feature Point(&quot; &lt;&lt; x &lt;&lt; &quot;,&quot; &lt;&lt; y &lt;&lt; &quot;)\n&quot;;
  }
// desctr
Pt_2d::~Pt_2d()
  {
    std::cout &lt;&lt; &quot;Drop Point &quot;;
  }

// ------------------------------------ Line_2d.h --
#ifndef LINE_2D_H_
#define LINE_2D_H_
#include &quot;GeoObj.h&quot;
#include &quot;Pt_2d.h&quot;
#include &lt;string&gt;

class Line_2d : public GeoObj
{
private:
  Pt_2d *pt1;
  Pt_2d *pt2;

public:
// constr
  Line_2d(std::string newname, Pt_2d &amp;pt1, Pt_2d &amp;pt2, long int idx);
  // destr
  ~Line_2d();

};
#endif /*LINE_2D_H_*/

// ------------------------------------ Line_2d.cpp --
#include &quot;Pt_2d.h&quot;
#include &quot;Line_2d.h&quot;
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;
// constr
Line_2d::Line_2d(std::string newname, Pt_2d::Pt_2d newp1, Pt_2d::Pt_2d newp2, long int idx)
             : Pt_2d(newp1, idx)
  {
//    pt1 = newp1;
//    pt2 = newp2;
//    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;)\n&quot;;
  }
// destr
Line_2d::~Line_2d()
  {
    std::cout &lt;&lt; &quot;Drop Line &quot;;
  }

// ------------------------------------ main.cpp --
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &quot;Pt_2d.h&quot; 
#include &quot;Line_2d.h&quot; 
#include &quot;GeoObj.h&quot;
#include &quot;reports.h&quot;

using namespace std;
int main()
{
  cout &lt;&lt; &quot;\n\n&quot; &lt;&lt; &quot;First OOP...&quot; &lt;&lt; &quot;\n\n&quot;;
  // create two pts and one line from 2 pts
  Pt_2d p1(&quot;Punkt1&quot;, 0, 4, 0);
  Pt_2d p2(&quot;Punkt2&quot;, 1, 1, 1);
  Line_2d l1(&quot;Line1&quot;,p1,p2,2);
  // Verweise auf Objekte in Container speichern
  vector&lt;GeoObj*&gt; GeoBaseObj(0);
  GeoBaseObj.push_back( &amp;p1 );
  GeoBaseObj.push_back( &amp;p2 );
  GeoBaseObj.push_back( &amp;l1 );

  return 0;
}

// ------------------------------------ The make file:
makefile:
all: hello

clean:
        -rm main.o Pt_2d.o Line_2d.o GeoObj.o reports.o hello

hello: main.o
        g++ -g -o hello main.o Pt_2d.o Line_2d.o GeoObj.o reports.o

main.o: main.cpp
        g++ -c -g main.cpp Pt_2d.cpp Line_2d.cpp GeoObj.cpp reports.cpp
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018025</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Mon, 07 Feb 2011 22:19:58 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Mon, 07 Feb 2011 23:38:00 GMT]]></title><description><![CDATA[<p>Wenn du es nicht gebacken bekommst, solltest du es mit braten probieren <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Etwas genauer könnte deine Problembeschreibung schon sein ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018041</guid><dc:creator><![CDATA[Manni66]]></dc:creator><pubDate>Mon, 07 Feb 2011 23:38:00 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Mon, 07 Feb 2011 23:54:02 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>selbst mit duensten krieg ichs nicht frittiert <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>
<p>Ja, ich weiss nicht, wie ich die Punktklasse als Membervariablen in die Linienklasse einbette. Das Problem liegt evtl. im Implementierungsteil, zumindest meckert der Compiler in diese Richtung:</p>
<blockquote>
<p>&gt; g++ -c -g main.cpp Pt_2d.cpp Line_2d.cpp GeoObj.cpp reports.cpp<br />
Line_2d.cpp:20: error: prototype for ‘Line_2d::Line_2d(std::string, Pt_2d, Pt_2d, long int)’ does not match any in class ‘Line_2d’<br />
Line_2d.h:9: error: candidates are: Line_2d::Line_2d(const Line_2d&amp;)<br />
Line_2d.h:18: error: Line_2d::Line_2d(std::string, Pt_2d&amp;, Pt_2d&amp;, long int)</p>
</blockquote>
<p>Mit meinem Miniwissen tippe ich mal auf ein Problem mit Zeiger und oder Namensraum?</p>
<p>Torsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018044</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Mon, 07 Feb 2011 23:54:02 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Tue, 08 Feb 2011 00:35:11 GMT]]></title><description><![CDATA[<p>Die Signatur von Deklaration und Definition vom Konstruktor ist unterschiedlich. In der Deklaration (in der Header-Datei) gibst du als Parameter Referenzen auf Punkte an, in der Definition (cpp-Datei) einfach &quot;nur&quot; Punkte. Da fehlt das &quot;&amp;&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018052</guid><dc:creator><![CDATA[fdfdg]]></dc:creator><pubDate>Tue, 08 Feb 2011 00:35:11 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Tue, 08 Feb 2011 01:07:43 GMT]]></title><description><![CDATA[<p>Hi fdfdg,<br />
danke, der Compiler schweigt!. Beim vielen rumfriemeln hatte sich gleich noch ein anderer Fehler eingeschlichen.<br />
Zumindest haut die Initialisierung hin und die Reportmethode der Basisklasse sieht das Linien-Objekt... Fuer den Anfaenger, wie mich haenge ich nochmal den berichtigten Implementierungsteil der Linienklasse an.</p>
<p>Dank &amp; Gruss, Torsten</p>
<pre><code class="language-cpp">// ------------------------------------ Line_2d.cpp --
#include &quot;Pt_2d.h&quot;
#include &quot;Line_2d.h&quot;
#include &quot;GeoObj.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;

// constr
Line_2d::Line_2d(std::string newname, Pt_2d::Pt_2d &amp;newp1, Pt_2d::Pt_2d &amp;newp2, long int idx)
             : GeoObj(newname, idx)
  {
    pt1 = &amp;newp1;
    pt2 = &amp;newp2;
    //std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;)\n&quot;;
  }

// destr
Line_2d::~Line_2d()
  {
    std::cout &lt;&lt; &quot;Drop Line &quot;;
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018054</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Tue, 08 Feb 2011 01:07:43 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Tue, 08 Feb 2011 02:29:18 GMT]]></title><description><![CDATA[<p>Also etwas als Referenz entgegenzunehmen, und sich dann einen Zeiger darauf zu merken, ist schon relativ ... eigenartig.<br />
Soll heissen: jemand der mit einer Klasse arbeiten soll, die sowas macht, wird wohl kaum damit rechnen, und u.U. die übergebenen Punkte löschen bevor er die Linie löscht.</p>
<p>Warum nicht einfach die Punkte &quot;by value&quot; in der Linie speichern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018062</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 08 Feb 2011 02:29:18 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Tue, 08 Feb 2011 09:37:33 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">Line_2d::Line_2d(std::string newname, Pt_2d::Pt_2d &amp;newp1, Pt_2d::Pt_2d &amp;newp2, long int idx) // warum Pt_2d::Pt_2d ? Pt_2d reicht
</code></pre>
<p>So wie du deine Klassen anlegst muss man erst 2 Punkte erschaffen und dann eine Linie. Die erschaffenen Punkte müssen im Scope bleiben wenn die Linie gültig sein will, das ist bis jetzt kein Problem - im Endeffekt bekommst du dann aber Seiteneffekte - sobald sich einer der Punkte ändert, ändert sich auch die Linie. Ist das beabsichtigt? Wenn nicht, guck dir nochmal Parameterübergabe perValue und perReference an.</p>
<p>Was meinst du passiert wenn jemand das hier macht:</p>
<pre><code class="language-cpp">Line_2d line(&quot;Kurve&quot;, Pt_2d(1,1,1), Pt_2d(5,4,2), 3);
</code></pre>
<p>Das mit den Ids ist auch unpraktisch ... der User könnte einfach gleiche IDs verteilen ... wenn du die Objecte eindeutig über die Id ansprechen magst kapsel das in dem BaseObj:</p>
<pre><code class="language-cpp">// base.h
class Base {
  static int maxIdSoFar;
  int id;
public:
 Base() : id(maxIdSoFar++) {}
};
// !!!! base.cpp
#include &quot;base.h&quot;

int Base::maxIdSoFar = 0;
</code></pre>
<p>Wenn du Verständnigsprobleme hast, melde dich. Sollte die ID eher eine Art &quot;Gruppierungsfunktion&quot; haben, nenn sie besser nicht id sondern wasauchimmer und verpass ihr nen defaultwert im Konstruktor für &quot;mir ist egal welcher gruppe dieses Objekt angehört&quot; ala:</p>
<pre><code class="language-cpp">class Base1 {
  int wasauchimmer;
  std::string name;
public:
  Base1(std::string NaMe, int WasAuchImmer=0) : name(NaMe), wasauchimmer(WasAuchImmer) { .... }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018135</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018135</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Tue, 08 Feb 2011 09:37:33 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Wed, 09 Feb 2011 00:02:23 GMT]]></title><description><![CDATA[<p>Hi hustbaer, padreigh...</p>
<p>danke fuer eure Hinweise! Ihr puhlt in der Wunde <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="😃"
    /> Als &quot;Durchschnittsscriptschreiber&quot; muss ich mich erstmal ein bischen in C++ zurechtfinden.</p>
<p>Mein Ziel ist eine Art strikte Objekthierarchie (bin kein Master of the oop-Terminologie). Meine Annahmen sind:<br />
# ========================================================================<br />
// -- Basisklasse --------------------------------------------------------<br />
Container fuer Datenhaltung/Reports &amp; (noch) keine Ahnung was noch...</p>
<p>// -- Punktklasse --------------------------------------------------------<br />
Sollte immer zuerst erzeugt werden; enthaelt punktspezifische Methoden...</p>
<p>// -- Linien-/Flaechenklassen --------------------------------------------<br />
Diese sollten nur die Relationen zwischen den Punkten enthalten sowie linien- und flaechenspezifische Methoden... D.h. z.B., dass die Lageaenderung eines Punktes gleichzeitig das Linien- oder Flaechenobjekt betrifft. Wird eine Linie geloescht, betrifft das gleichzeitig die Punkte. Wird ein Punkt geloescht, muessen (irgendwie) die Relationen der betreffenden hoeherhierarchischen Objekte &quot;on-the-fly&quot; upgedated werden, incl. Gueltigkeitsprüfung. D.h. eine Fläche muesste nach einem &quot;Punktupdating&quot; automatisch checken, ob sie noch eine Flaeche ist. (...sonst selfkill of Flaeche <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> ). Daher muss man wohl intensiv mit Zeigern/Referenzen arbeiten... Und ich nur Freizeitscriptschreiber... <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="😃"
    /><br />
# ========================================================================</p>
<p>Soweit der Hintergrund zu meinem c++-Training. Ich habe jetzt die Wertuebergabe wie folgt gestaltet:</p>
<pre><code class="language-cpp">#ifndef LINE_2D_H_
#define LINE_2D_H_

#include &quot;GeoObj.h&quot;
#include &quot;Pt_2d.h&quot;
#include &lt;string&gt;

class Line_2d : public GeoObj
{

private:
  Pt_2d *pt1;
  Pt_2d *pt2;

public:
  // constr
  Line_2d(std::string newname, Pt_2d newp1, Pt_2d newp2, long int idx);

  std::string report_single();

  // destr
  ~Line_2d();
};
#endif /*LINE_2D_H_*/
</code></pre>
<p>Im Implementationsteil kann ich aber nicht mehr auf die eigentlichen Punktobjekte zugreifen:</p>
<pre><code class="language-cpp">Line_2d::Line_2d(std::string newname, Pt_2d newp1, Pt_2d newp2, long int idx)
             : GeoObj(newname, idx)
  {
    pt1 = &amp;newp1;
    pt2 = &amp;newp2;
    // DAS GEHT...
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &amp;newp1 &lt;&lt; &quot;)\n&quot;;
    // DAS GEHT NICHT
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; *pt1 &lt;&lt; &quot;)\n&quot;;
    cout &lt;&lt; newp1.Pt_2d::report_single();
  }
</code></pre>
<p>Was mache ich falsch??? Warum kann ich nicht mit dem *pt1-Zeiger arbeiten???</p>
<p>THX, Torsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2018535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018535</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Wed, 09 Feb 2011 00:02:23 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Wed, 09 Feb 2011 07:46:06 GMT]]></title><description><![CDATA[<p>Weil es in c++ Unterschiede gibt je nach dem was du machst, ganz Grob (da du offensischtlich nicht nach perRef perVal googlen magst:</p>
<p>kompilier das hier mal und Spiele rum, verstehen was wo warum crashed:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

void aus(int a, int &amp;b, int * c)
{
  std::cout &lt;&lt; &quot;i, j, k, *k&quot; &lt;&lt; i &lt;&lt; &quot;, &quot; &lt;&lt; j &lt;&lt; &quot;, &quot;  &lt;&lt; k &lt;&lt; &quot;, &quot;;
  if (k!=0)
    std::cout &lt;&lt; *k;
  else 
    std::cout &lt;&lt; &quot;n.a.&quot;; 
  std::cout &lt;&lt; std::endl; 
}

void m(int a, int &amp;b, int * c)
{
   a += 10;
   b *= 2;
   *c  = 1000;
}

int main() {
  int i = 21;
  int j = 22;
  int * k = 0;

  aus(10,20,0);
  m(10,20,0);

 aus(i,j,0);
  m(i,j,&amp;j);
 aus(i,j,0);

  aus(i,j,k);
  m(i,j,k);
  aus(i,j,k);

  k = new int(24);
  aus(i,j,k);
  m(i,j,k);
  aus(i,j,k);

  delete k;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018550</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Wed, 09 Feb 2011 07:46:06 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Wed, 09 Feb 2011 23:55:38 GMT]]></title><description><![CDATA[<p>Ich bin zwar nicht Mr. Zeiger, aber das Grundprinzip von Zeigern etc. habe ich verstanden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Bei meinen Objekten bin ich mir aber trotzdem nicht sicher...<br />
Ich glaube folgendes zu tun:</p>
<ol>
<li>
<p>Ich kreiere 2 Punkte und eine Linie mit jenen in main():<br />
Pt_2d p1(&quot;Punkt1&quot;, 0, 4, 0);<br />
Pt_2d p2(&quot;Punkt2&quot;, 1, 1, 1);<br />
Line_2d l1(&quot;Line1&quot;,p1,p2,2);</p>
</li>
<li>
<p>Ich uebergebe fuer die Initialisierung des Linienobjektes die beiden Punkte<br />
als (FRAGE:) Zeiger?...<br />
<strong>this</strong> von z.B. p1 enthaelt eine andere Adresse als <strong>&amp;newp1</strong></p>
</li>
<li>
<p>Ich weise dem Zeiger *<strong>pt1</strong> (FRAGE:) die Adresse meines<br />
Punktobjektes <strong>pt1 = &amp;newp1;</strong> zu?</p>
</li>
</ol>
<p>Rufe ich jetzt eine report-Methode in der Punkt-Klasse auf, geht es zwar mit &amp;newpt1 aber nicht mit pt1 (siehe Codeschnipsel #1). Laut Compileroutput zeigt pt1 mitnichten auf das Punktobjekt:</p>
<p>$&gt; g++ -c -g main.cpp Pt_2d.cpp Line_2d.cpp GeoObj.cpp reports.cpp<br />
Line_2d.cpp: In constructor ‘Line_2d::Line_2d(std::string, Pt_2d, Pt_2d, long int)’:<br />
Line_2d.cpp:37: error: request for member ‘Pt_2d::report_single’ in ‘((Line_2d*)this)-&gt;Line_2d::pt1’, which is of non-class type ‘Pt_2d*’</p>
<p>Entweder geht das generell nicht so, wie gedacht, oder ich hab mich im Adressraum verirrt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> ?</p>
<p>Danke, Torsten</p>
<pre><code class="language-cpp">// Headerdatei fuer Linienobjekt
class Line_2d : public GeoObj
{

private:
  Pt_2d::Pt_2d *pt1;
  Pt_2d::Pt_2d *pt2;

public:
  // constr
  Line_2d(std::string newname, Pt_2d::Pt_2d newp1, Pt_2d::Pt_2d newp2, long int idx);

  // destr
  ~Line_2d();

};

// Implementierungsdatei fuer Linienobjekt
  // constr
Line_2d::Line_2d(std::string newname, Pt_2d newp1, Pt_2d newp2, long int idx)
             : GeoObj(newname, idx)
  {
    pt1 = &amp;newp1;
    pt2 = &amp;newp2;
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &amp;newp1 &lt;&lt; &quot;)\n&quot;;
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; pt1 &lt;&lt; &quot;)\n&quot;;
    cout &lt;&lt; newp1.Pt_2d::report_single();
    // #1 Compiliert nicht
    cout &lt;&lt; pt1.Pt_2d::report_single();
  }
// destr
Line_2d::~Line_2d()
  {
    std::cout &lt;&lt; &quot;Drop Line &quot;;
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018991</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018991</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Wed, 09 Feb 2011 23:55:38 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Thu, 10 Feb 2011 00:30:16 GMT]]></title><description><![CDATA[<p>Du solltest wirklich mal nach call by value, call by reference googeln, dir anschauen was temporäre objekte sind und wie man memberfunktionen über pointer aufruft.</p>
<p>Was du möchtest ist sicherlich in etwa folgendes:</p>
<pre><code class="language-cpp">// Headerdatei fuer Linienobjekt
class Line_2d : public GeoObj {
private:
  Pt_2d *pt1, *pt2;

public:
  Line_2d(std::string newname, Pt_2d &amp;newp1, Pt_2d &amp;newp2, long int idx);
  ~Line_2d();
};

// Implementierungsdatei fuer Linienobjekt
Line_2d::Line_2d(std::string newname, Pt_2d &amp;newp1, Pt_2d &amp;newp2, long int idx)
       : GeoObj(newname, idx), pt1(&amp;newp1), pt2(&amp;newp2)
  {
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; &amp;newp1 &lt;&lt; &quot;)\n&quot;;
    std::cout &lt;&lt; &quot;Create Feature Line(&quot; &lt;&lt; &quot;test&quot; &lt;&lt; &quot;,&quot; &lt;&lt; pt1 &lt;&lt; &quot;)\n&quot;;
    cout &lt;&lt; newp1.report_single();
    // Compiliert auch
    cout &lt;&lt; pt1-&gt;report_single();
  }
// destr
Line_2d::~Line_2d() {
    std::cout &lt;&lt; &quot;Drop Line &quot;;
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018997</guid><dc:creator><![CDATA[inter2k3]]></dc:creator><pubDate>Thu, 10 Feb 2011 00:30:16 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Thu, 10 Feb 2011 01:26:49 GMT]]></title><description><![CDATA[<p>Hi inter2k3,</p>
<p>1000Dank! Der &quot;Pfeil&quot; macht den Unterschied... Es kompiliert und meine Punktmethoden funktionieren. Bischen unklar ist mir noch, warum beide Konstruktoren (siehe unten) bisher nichts am generellen Verhalten aendern. Ich kann mit beiden Versionen meine Punkte verschieben, neu setzen usw. Mein C++-Buch sagte mir, dass die zweite Variante dazu da ist, automatisch (in diesem Fall) zwei neue Punktobjekte zu kreieren. Andererseits wird ja auf eine bestehende Adresse verwiesen. Puhh...<br />
Danke fuer die Suchtips.</p>
<p>Torsten</p>
<pre><code class="language-cpp">Line_2d::Line_2d(std::string newname, Pt_2d &amp;newp1, Pt_2d &amp;newp2, long int idx)
             : GeoObj(newname, idx)

// ERGIBT DAS GLEICHE, WIE:

Line_2d::Line_2d(std::string newname, Pt_2d &amp;newp1, Pt_2d &amp;newp2, long int idx)
             : GeoObj(newname, idx), pt1(&amp;newp1), pt2(&amp;newp2)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2019002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2019002</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Thu, 10 Feb 2011 01:26:49 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Thu, 10 Feb 2011 09:06:39 GMT]]></title><description><![CDATA[<p>Du machst es dir echt schwer ... und schränkst deine Klassen stark ein - Respektive schaffst dir schwer zu debuggende Fehlerquellen.</p>
<p>Dein Ansatz sorgt dafür das die Punkte die du zum Konstruieren der Linie benutzt IMMMER im Scope bleiben müssen damit deren Adressen noch haltbar sind - alternativ müsstest du die Punkte mit &quot;new&quot; anlegen - und dann sage ich dir Speicherlecks ohne Ende vorher. Es wäre sauberer, wenn eine Linie eigene Kopien der Punkte bekommt, die dann auch unabhängig von den Punkten sind mit denen du sie &quot;erschaffen&quot; hast. Wenn du gerne die Punkte einer Linie als Punkte umherschieben willst, solltest du sie dir vorher von der Linie wieder holen.</p>
<p>Compilier und start mal ... und denk nach:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct Punkt {
  int x;
  int y;
  Punkt(int X,int Y) : x(X), y(Y) {std::cout &lt;&lt; &quot;Construct:&quot;; report();}
  ~Punkt() { std::cout &lt;&lt; &quot;Destruct:&quot;; report(); }

  void report()
  {
      std::cout &lt;&lt; &quot;\t(&quot; &lt;&lt; x &lt;&lt; &quot;,&quot; &lt;&lt; y &lt;&lt; &quot;)\tadressX &quot; &lt;&lt; &amp;x &lt;&lt; &quot;\tadressY&quot; &lt;&lt; &amp;y &lt;&lt; std::endl;
  }
};

struct Linie {
  Punkt * p1;
  Punkt * p2;
  Linie (Punkt &amp; P1, Punkt &amp; P2) : p1(&amp;P1), p2(&amp;P2) {}
};

int main()
{
   // das hier geht
    Punkt ol1 (10,10);
    Punkt ur1 (20,20);
   Linie l1 (ol1,ur1);
   Linie l2 (l1);
   // das hier geht nicht:
   {
       Punkt ol2 (1,1);
       Punkt ur2 (2,2);
       l2 = Linie (ol2,ur2);
   }

   // std::cout &lt;&lt; ol2.x;

   std::cout &lt;&lt; &quot;Using:     &quot;;
   l2.p1-&gt;report();
}
</code></pre>
<p>Das ganze läuft bei mir in dem Beispiel überigens ohne Fehler und mit richtigem Ergebnis ... überleg mal warum.</p>
<p>Besser wäre daher:</p>
<pre><code class="language-cpp">struct Punkt {
  int x;
  int y;
  Punkt(int X,int Y) : x(X), y(Y) {std::cout &lt;&lt; &quot;Construct:&quot;; report();}
  Punkt(const Punkt &amp; p) {std::cout &lt;&lt; &quot;Copying:&quot;; p.report(); x = p.x; y = p.y;}
  ~Punkt() { std::cout &lt;&lt; &quot;Destruct:&quot;; report(); }

  void report() const
  {
      std::cout &lt;&lt; &quot;\t(&quot; &lt;&lt; x &lt;&lt; &quot;,&quot; &lt;&lt; y &lt;&lt; &quot;)\tadressX &quot; &lt;&lt; &amp;x &lt;&lt; &quot;\tadressY&quot; &lt;&lt; &amp;y &lt;&lt; std::endl;
  }
};

struct Linie {
private:
  Punkt p1;
  Punkt p2;
public:
  Linie (const Punkt &amp; P1, const Punkt &amp; P2) : p1(P1), p2(P2) {}

  Punkt &amp; startPunkt() { return p1; }
  Punkt &amp; endPunkt() { return p2; }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2019024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2019024</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Thu, 10 Feb 2011 09:06:39 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Thu, 10 Feb 2011 23:41:37 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23744">@padreigh</a>: Das &quot;besser waere daher&quot;-Beispiel laeuft nun auch nicht grade rund. War wahrscheinlich eine Aufgabe fuer mich.</p>
<p>Trotzdem Danke @moment, ich versuch erstmal weiter zu friemeln...</p>
<p>Torsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2019422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2019422</guid><dc:creator><![CDATA[Dorstne]]></dc:creator><pubDate>Thu, 10 Feb 2011 23:41:37 GMT</pubDate></item><item><title><![CDATA[Reply to eingebettete Objekte on Fri, 11 Feb 2011 04:10:47 GMT]]></title><description><![CDATA[<p>Dorstne schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23744">@padreigh</a>: Das &quot;besser waere daher&quot;-Beispiel laeuft nun auch nicht grade rund. War wahrscheinlich eine Aufgabe fuer mich.</p>
<p>Trotzdem Danke @moment, ich versuch erstmal weiter zu friemeln...</p>
<p>Torsten</p>
</blockquote>
<p>Doch tut es <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Du musst natürlich die Zeile:</p>
<pre><code class="language-cpp">l2.p1-&gt;report();        // in main
</code></pre>
<p>ersetzen durch:</p>
<pre><code class="language-cpp">l2.startPunkt().report();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2019437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2019437</guid><dc:creator><![CDATA[inter2k3]]></dc:creator><pubDate>Fri, 11 Feb 2011 04:10:47 GMT</pubDate></item></channel></rss>