<?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[C++ Klassen Frage]]></title><description><![CDATA[<p>Hi,<br />
ich möchte gerne eine Klasse erstellen die Werte hat. Diese Werte möchte ich aber in einer anderen Klasse auch nutzen.</p>
<pre><code class="language-cpp">class no1{
 private:
  int i;
 public:
  no1(){
  i = 10;
 }

 inline get_i()
   return this-&gt;i;

};

class no2{
 private:
  int iMerken;
 public:
  rechne(int i, int iMerke)
    this-&gt;iMerke = i+iMerke;
};
</code></pre>
<p>So ungefähr am besten das ich das nicht als Parameter erstellen muss sondern das ich da immer vollen Zugriff hab. Also nur beim erstellen angeben muss.<br />
Das int ist im meinen Projekt eine struct also ein bisschen größer der ganze Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309072/c-klassen-frage</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 12:09:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309072.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Oct 2012 12:46:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 12:46:49 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich möchte gerne eine Klasse erstellen die Werte hat. Diese Werte möchte ich aber in einer anderen Klasse auch nutzen.</p>
<pre><code class="language-cpp">class no1{
 private:
  int i;
 public:
  no1(){
  i = 10;
 }

 inline get_i()
   return this-&gt;i;

};

class no2{
 private:
  int iMerken;
 public:
  rechne(int i, int iMerke)
    this-&gt;iMerke = i+iMerke;
};
</code></pre>
<p>So ungefähr am besten das ich das nicht als Parameter erstellen muss sondern das ich da immer vollen Zugriff hab. Also nur beim erstellen angeben muss.<br />
Das int ist im meinen Projekt eine struct also ein bisschen größer der ganze Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259947</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259947</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sat, 13 Oct 2012 12:46:49 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 13:02:09 GMT]]></title><description><![CDATA[<p>Ich versteh nicht ganz, was dein Beispiel mit der Frage zu tun hat. Abgesehen davon enthält dein Beispiel viele Fehler und ist nichtmal kompilierbar. Willst du, dass alle Instanzen einer Klasse auf alle Instanzen einer anderen Klasse bzw. deren private-Elemente zugreifen dürfen? Dann mach die eine Klasse zum friend der anderen. Willst du eine Hierarchie aufbauen? Dann nimm Vererbung. Willst du, dass eine Instanz einer Klasse auf eine Instanz der anderen Klasse zugreifen kann? Dann nimm friend und einen Zeiger auf die jeweilige Instanz.<br />
Aber es ist allgemein kein gutes Design, wenn sich eine Klasse einer anderen vollkommen offenbart.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259952</guid><dc:creator><![CDATA[Der Tobi]]></dc:creator><pubDate>Sat, 13 Oct 2012 13:02:09 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 13:27:53 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class no1 {

	private:
		int i;

	public:
		no1() : i( 10 ) {}
		int get_i() const { return i; }
};

class no2 {

	private:
		no1 const &amp;the_other_f_cking_instance_i_depend_on;
		int iMerken;

	public:
		no2( no1 const &amp;the_other_f_cking_instance_i_depend_on ) : the_other_f_cking_instance_i_depend_on( the_other_f_cking_instance_i_depend_on ) {}

		void rechne( int i )
		{
			iMerken = i + the_other_f_cking_instance_i_depend_on.get_i();
		}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2259962</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259962</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 13:27:53 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 14:07:47 GMT]]></title><description><![CDATA[<p>Ok ich hattes das von Handy geschrieben also von da her sorry. <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 />
Ich möchte halt bei der Klasse It auf die Instanzen *first und *last von der Klasse myList zugreifen. Wie stelle ich das am besten an. Ich möchte halt eine Liste erstellen und mit der Klasse It zu einen entsprechenden Element gehen zb das 10.<br />
Hoffe ihr versteht jetzt was ich gerne erreichen möchte.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

struct Element{
	int data;
	Element *prev;
};

class myList{
	private:
		Element *first;
		Element *last;

	public:
		myList(){
			first = new Element;
			last = first;
			first[0].data = 0;
		}

		void push_back(int in){
			last = new Element;
			last[0].data = in;
			first[0].prev = this-&gt;last;
		}

		void show(){
			cout &lt;&lt; last[0].data &lt;&lt; endl;
		}

};

class it{
	private:
		Element Aktuell;
	public:

};

int main(){

	myList list;

	for(int i = 1; i &lt; 200; i++){
		list.push_back(i);
		list.show();
	}

	_getch();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2259982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259982</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sat, 13 Oct 2012 14:07:47 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 17:25:09 GMT]]></title><description><![CDATA[<p>Lass zuerst mal deine Liste kritisieren, danach kann man über den Iterator reden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260067</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 17:25:09 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 17:38:18 GMT]]></title><description><![CDATA[<p>Von mir aus kannst loslegen mit den Kritisieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260075</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sat, 13 Oct 2012 17:38:18 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 18:00:19 GMT]]></title><description><![CDATA[<p>Mehr als oben in dem Codefetzen steht hast noch nicht?</p>
<p>CraftPlorer schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

struct Element{
	int data;
	Element *prev; // warum heiszt das Ding prev?
};

class myList{
	private:
		Element *first;
		Element *last;

	public:
		myList(){
			first = new Element; // warum hat eine leere Liste ein Element?
			last = first;
			first[0].data = 0; // brrr ... first-&gt;data !?
		}

        void push_back(int in){
            last = new Element;
            last[0].data = in; // siehe oben.
            first[0].prev = this-&gt;last; // Nach push_back() ist die liste kaputt. Wohin zeigt last-&gt;prev?
        }

        void show(){ // eine funktion, die nur das letzte element ausgibt?
            cout &lt;&lt; last[0].data &lt;&lt; endl; // siehe oben.
        }

};
</code></pre>
</blockquote>
<p>Warum dereferenzierst du immer mitm Indexoperator anstatt mit dem Dereferenzierungsoperator?<br />
Warum hat das Ding keinen Destruktor?<br />
Was ist an <code>push_back()</code> kaputt. Wie geht's richtig?<br />
Wie greifst auf den Wert des <code>i</code> ten Listenelements zu? Lesend und schreibend? Mal ohne Iterator?<br />
Plötzlich kommst d'rauf, du hättest gerne auch eine <code>myList</code> für <code>float</code> - was tust?</p>
<p>//edit: tagsoup ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260082</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260082</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 18:00:19 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 20:52:24 GMT]]></title><description><![CDATA[<p>Willst du eine einfach verkettete Liste? Also da hab ich es so gelernt, dass man einen Kopfknoten und einen Endknoten hat, die beide keine Daten enthalten. Jeder Knoten speichert nur einen Zeiger zu seinem Nachfolger und einen Zeiger/Referenz zu den Daten. Wenn du willst kann ich ja mal meine Implementierung einer verketteten Liste posten, zwar noch ohne Templates, aber die sind dann ja ein Kinderspiel. So wie du das versuchst wird das keine Liste, sondern allerhöchstens ein C-Abklatsch mit bisschen new/delete.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260143</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260143</guid><dc:creator><![CDATA[Der Tobi]]></dc:creator><pubDate>Sat, 13 Oct 2012 20:52:24 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sat, 13 Oct 2012 21:12:53 GMT]]></title><description><![CDATA[<p>Der Tobi schrieb:</p>
<blockquote>
<p>Also da hab ich es so gelernt, dass man einen Kopfknoten und einen Endknoten hat, die beide keine Daten enthalten.</p>
</blockquote>
<p>Wieso sollen Wurzel und Ende keine Daten tragen?</p>
<p>Der Tobi schrieb:</p>
<blockquote>
<p>Jeder Knoten speichert nur einen Zeiger zu seinem Nachfolger und einen Zeiger/Referenz zu den Daten.</p>
</blockquote>
<p>Warum &quot;Zeiger/Referenz zu den Daten&quot; und nicht gleich die Daten selbst?</p>
<p>Der Tobi schrieb:</p>
<blockquote>
<p>Wenn du willst kann ich ja mal meine Implementierung einer verketteten Liste posten, [...] So wie du das versuchst wird das keine Liste, sondern allerhöchstens ein C-Abklatsch mit bisschen new/delete.</p>
</blockquote>
<p>Zeig' mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260151</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 21:12:53 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sun, 14 Oct 2012 01:31:41 GMT]]></title><description><![CDATA[<p>Ist sicherlich in keiner Weise perfekt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;list.h&quot;
//main.cpp

int main()
{
    List list;

    for (int i = 0; i&lt;1000; ++i)
        list.insert(i);

    list.print();
}
</code></pre>
<pre><code class="language-cpp">//list.h
#include &quot;node.h&quot;
#include &quot;startnode.h&quot;
#include &quot;endnode.h&quot;

class List
{
    Startnode* _start;

    List(const List&amp;);
    List&amp; operator=(const List&amp;);

    public:

        List();

        ~List();

        void insert(const Data&amp;);

        void print() const;
};
</code></pre>
<pre><code class="language-cpp">#include &quot;list.h&quot;
#include &lt;iostream&gt;
//list.cpp
List::List()
    : _start(new Startnode(new Endnode))
{
}

List::~List()
{
    delete _start;
}

void List::insert(const Data&amp; dat)
{
    _start-&gt;insert(dat);
}

void List::print() const
{
    _start-&gt;print();
}
</code></pre>
<pre><code class="language-cpp">//startnode.h
#include &quot;node.h&quot;

class Startnode : public bNode
{
    bNode* _next;

    Startnode(const Startnode&amp;);
    const Startnode&amp; operator=(const Startnode&amp;);

    public:

        Startnode(bNode*);

        ~Startnode();

        bool insert(const Data&amp;);

        void print() const {_next-&gt;print();}
};
</code></pre>
<pre><code class="language-cpp">#include &quot;startnode.h&quot;
#include &lt;iostream&gt;
//startnode.cpp
Startnode::Startnode(bNode* next)
           : _next(next)
{
}

Startnode::~Startnode()
{
    delete _next;
}

bool Startnode::insert(const Data&amp; dat)
{
    if (!_next-&gt;insert(dat))
        _next = new Node(dat, _next);
    return true;
}

const Startnode&amp; Startnode::operator=(const Startnode&amp; rhs)
{
    *(this-&gt;_next) = *(rhs._next);
    return *this;
}
</code></pre>
<pre><code class="language-cpp">#include &quot;data.h&quot;

class bNode
{
    public:
        virtual ~bNode() {}
        virtual bool insert(const Data&amp;) = 0;
        virtual void print() const  = 0;
};

//node.h
class Node : public bNode
{
    Data* _data;
    bNode* _next;

    const Node&amp; operator=(const Node&amp;);
    Node(const Node&amp;);

    public:
        Node(const Data&amp;, bNode*);

        ~Node();

        bool insert(const Data&amp;);

        void print() const;
};
</code></pre>
<pre><code class="language-cpp">#include &quot;node.h&quot;
#include &lt;iostream&gt;
//node.cpp
Node::Node(const Data&amp; dat, bNode* next)
        : _data(new Data(dat)), _next(next)
{
}

Node::~Node()
{
    delete _next;
    delete _data;
}

bool Node::insert(const Data&amp; dat)
{
    if (!_next-&gt;insert(dat)) //if insert returns false for the next node (can only happen if next node is
        _next = new Node(dat, _next); // end node, because Node::insert always returns true) insert is
    return true; //called for the next node -&gt; all nodes are added directly before the end node
}

void Node::print() const
{
    std::cout &lt;&lt; &quot;Wert: &quot; &lt;&lt; _data-&gt;Value() &lt;&lt; &quot;\n&quot;;
    _next-&gt;print();
}
</code></pre>
<pre><code class="language-cpp">//endnode.h
#include &quot;node.h&quot;
#include &lt;iostream&gt;

class Endnode : public bNode
{
    public:

        ~Endnode() {}
        bool insert(const Data&amp; dat) {return false;}
        void print() const {}
};
</code></pre>
<pre><code class="language-cpp">//data.h
class Data
{
    int _value;

    public:
        Data(int = 0);
        ~Data();

        Data(const Data&amp;);

        const Data&amp; operator=(const Data&amp;);

        bool operator&lt;(const Data&amp;) const;
        bool operator==(const Data&amp;) const;
        bool operator&gt;(const Data&amp;) const;

        void Value(int v) {_value = v;}
        int Value() const {return _value;}
};
</code></pre>
<pre><code class="language-cpp">#include &quot;data.h&quot;
//data.cpp
Data::Data(int val)
        : _value(val)
{
}

Data::~Data()
{
}

Data::Data(const Data&amp; rhs)
        : _value(rhs._value)
{
}

const Data&amp; Data::operator=(const Data&amp; rhs)
{
    this-&gt;_value = rhs._value;
    return *this;
}

bool Data::operator&lt;(const Data&amp; rhs) const
{
    return (this-&gt;_value &lt; rhs._value);
}

bool Data::operator==(const Data&amp; rhs) const
{
    return (this-&gt;_value == rhs._value);
}

bool Data::operator&gt;(const Data&amp; rhs) const
{
    return (this-&gt;_value &gt; rhs._value);
}
</code></pre>
<p>Ja, jetzt wo ich es mir nochmal anschaue war die abstrakte Basisklasse doch irgendwie ein Overkill. Warte, ich setz mich nochmal ran.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260184</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260184</guid><dc:creator><![CDATA[Der Tobi]]></dc:creator><pubDate>Sun, 14 Oct 2012 01:31:41 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sun, 14 Oct 2012 14:00:12 GMT]]></title><description><![CDATA[<p>Also das von Tobi versteh ich gar nicht...<br />
Ich hab mich noch mal selber ran gesetzt und hab jetzt 3 Probleme.<br />
1. Bei der Klasse it fehlt halt das first also das erste Element.<br />
Könnte ich natürlich ganz einfach mit Parameter lösen aber kann man das nicht so machen das man die Klasse so erstellt das sie alle werte bekomme von einer andern also beim ersten erstellen(myList list; it It;) das man da angibt das It zu list gehört?</p>
<p>2. Wie mach ich das das es nicht nur für int funktioniert? Ich meine wenn ich ein Pointer erstelle muss ich doch angeben von welchen Typ...</p>
<p>3. Ich bin mir ziemlich sicher das der Destructor falsch ist also das mit den löschen. Wie sieht das richtig aus?</p>
<p>Der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

struct Element{
	int data;
	Element *last;
	Element *next;
};

class myList{
	private:
		Element *current;
		Element *before;

		Element *first;
		Element *last;

		bool    _New;
	public:
		myList(){
				current   = new Element;
				first     = current;
				last      = current;
				before	  = current;

				current-&gt;last = current;
				current-&gt;next = current;
		}

                ~myList(){
		       delete [] current;
		}

		void push_back(int in){

				if(_New){
					current-&gt;data = in;
					_New = false;
				}
				current       = new Element;

				before-&gt;next  = current;
				current-&gt;data = in;
				current-&gt;last = before;
				current-&gt;next = current;
				last		  = current;

				before        = current;
		}

		void show(){

			Element *loop;
			loop = first-&gt;next;
			cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;		&quot; &lt;&lt; loop &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;last 
			     &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl;

			do{
				loop = loop-&gt;next;
				cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;		&quot; &lt;&lt; loop &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;last 
				 &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl;
			}while(!(loop == last));

		}
};

//class it{
//	private:
//		Element *loop;
//		Element end;
//		
//	public:
//		void insert(int end){
//			myList classList;
//			loop = classList.first;
//			for(int i = 0; i &lt;= end; i++){
//				loop = loop-&gt;next;
//			}
//			cout &lt;&lt; loop-&gt;data &lt;&lt; endl;
//		}
//		
//};

int main(){

	myList list;

	for(int i = 0; i &lt;= 100; i++){
	list.push_back(i);
	}

	list.show();

	_getch();
	return 0;
}
</code></pre>
<p>Wer Fehler oder Verbesserungen hat bitte mit Zitat oder Zeilennummer antworten.<br />
<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 />
Hoffe ihr könnt mir helfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260289</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sun, 14 Oct 2012 14:00:12 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sun, 14 Oct 2012 15:42:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
/* #include &lt;conio.h&gt; kein std c++ */

using namespace std;

struct Element{
    int data;
    Element *last; // wtf?
    Element *next;
};

class myList{
	private:
		Element *current;	// -+
		Element *before;	// -+- wtf? 

		Element *first;
		Element *last;

		bool    _New; // &lt;- what? wird auch nicht initialisiert ...

	public:
		myList( )
		{
			current   = new Element;
			first     = current; // wtf? warum hat eine LEERE liste schon wieder ein Element?
			last      = current;
			before      = current;

			current-&gt;last = current;
			current-&gt;next = current;
		}

		~myList()
		{
			delete [] current; // hast du current mit new[] erzeugt? wenn nein: wtf?
		}

		void push_back( int in )
		{
			if( _New ) {

l				current-&gt;data = in;
				_New = false;
			}

			current = new Element;

			before-&gt;next = current;

			current-&gt;data = in;
			current-&gt;last = before;
			current-&gt;next = current;

			last = current;
			before = current;
        }

		void show()
		{
			Element *loop;
			loop = first-&gt;next; // warum ist first nicht das erste element? Warum initialisierst loop nicht direkt bei der Deklaration?
			cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;        &quot; &lt;&lt; loop &lt;&lt; &quot;        &quot; &lt;&lt; loop-&gt;last &lt;&lt; &quot;        &quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl; // was, wenn first-&gt;next == nullptr?

            do {
				loop = loop-&gt;next;
				cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;        &quot; &lt;&lt; loop &lt;&lt; &quot;        &quot; &lt;&lt; loop-&gt;last &lt;&lt; &quot;        &quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl;  // was, wenn loop-&gt;next == nullptr?
			} while( !(loop == last) );
		}
};

int main()
{
	myList list;

	for(int i = 0; i &lt;= /* 100 */ 10 /* reicht auch */; i++){
		list.push_back(i);
	}

	list.show();

	myList other_list;
	other_list.show(); // &lt;- ouch!

	other_list.push_back( 1 );
	other_list.show(); // &lt;- ouch!

	/* _getch(); kein std c++ */
	cin.get(); // tut's auch.
}
</code></pre>
<p>Warum initialisierst du Klassenmember nicht?<br />
Warum die Unmenge an Pointer?<br />
Warum hat ein <code>Element</code> einen Pointer aufs Ende ( <code>last</code> )?<br />
Verabschiede dich bitte von der Idee, direkt in die List einen Zeiger auf ein &quot;aktuelles&quot; Element ( &lt;- was das auch immer im Kontext einer Liste sein sollte) zu halten.</p>
<p>vielleicht solltest du dir die Definition einer einfach verketteten Liste ansehen: <a href="http://de.wikipedia.org/wiki/Liste_%28Datenstruktur%29#Einfach_verkettete_Liste" rel="nofollow">Einfach verkettete Liste</a></p>
<p>// edit: Geschmacksmuster:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class list_node_t
{
	private:

		int value;
		list_node_t *next;

	public:

		list_node_t() : value(), next( nullptr ) {}
		list_node_t( int value ) : value( value ), next( nullptr ) {}

		int get_value() const { return value; }
		list_node_t* get_next() const { return next; }
		void set_next( list_node_t * const new_next ) { next = new_next; }

	private:
		list_node_t( list_node_t const &amp;other ); // Unsere Listenknoten koennen nicht kopiert werden.
};

class list_t
{
	private:

		list_node_t *root;

	public:

		list_t() : root( nullptr ) {}

		~list_t()
		{
			std::cout &lt;&lt; &quot;\n~list_t()\n&quot;;
			if( root ) { // &lt;- wenn das Ding nicht sowieso leer ist ...

				list_node_t *to_delete = root;
				list_node_t *next = nullptr;

				do {

					next = to_delete-&gt;get_next();
					std::cout &lt;&lt; &quot;Deleting node with value &quot; &lt;&lt; to_delete-&gt;get_value() &lt;&lt; &quot;\n&quot;;
					delete to_delete;

				} while( to_delete = next ); // solange next nicht nullptr
			}						
		}

		void push_back( int value )
		{
			std::cout &lt;&lt; &quot;\npush_back( &quot; &lt;&lt; value &lt;&lt; &quot; )\n&quot;;

			if( !root ) { // sonderfall: Liste ist leer.

				std::cout &lt;&lt; &quot;Creating root node with value &quot; &lt;&lt; value &lt;&lt; '\n';
				root = new list_node_t( value );

			} else {

				list_node_t *i = root;

				for( ; i-&gt;get_next(); i = i-&gt;get_next() ) {

					std::cout &lt;&lt; &quot;Passed node with value &quot; &lt;&lt; i-&gt;get_value() &lt;&lt; '\n';
					// Bis ans Ende laufen
				}

				std::cout &lt;&lt; &quot;Passed node with value &quot; &lt;&lt; i-&gt;get_value() &lt;&lt; '\n';
				std::cout &lt;&lt; &quot;Ceating new last node with value &quot; &lt;&lt; value &lt;&lt; '\n';
				i-&gt;set_next( new list_node_t( value ) ); // am letzen Element anhängen
			}
		}

		void print() const
		{
			std::cout &lt;&lt; &quot;\nprint()\n&quot;;

			std::size_t counter = 1;

			for( list_node_t *i = root; i; i = i-&gt;get_next(), ++counter ) {

				std::cout &lt;&lt; &quot;List node #&quot; &lt;&lt; counter &lt;&lt; &quot; has value &quot; &lt;&lt; i-&gt;get_value() &lt;&lt; '\n';
			}
		}

	private:
		list_t( list_t const &amp;other ); // Unsere Liste kann nicht kopiert werden.
};

int main()
{
	list_t list;

	for( int i = 1; i &lt;= 5; ++i ) {

		list.push_back( i );
	}

	list.print();
}
</code></pre>
<p>Output:</p>
<pre><code>push_back( 1 )
Creating root node with value 1

push_back( 2 )
Passed node with value 1
Ceating new last node with value 2

push_back( 3 )
Passed node with value 1
Passed node with value 2
Ceating new last node with value 3

push_back( 4 )
Passed node with value 1
Passed node with value 2
Passed node with value 3
Ceating new last node with value 4

push_back( 5 )
Passed node with value 1
Passed node with value 2
Passed node with value 3
Passed node with value 4
Ceating new last node with value 5

print()
List node #1 has value 1
List node #2 has value 2
List node #3 has value 3
List node #4 has value 4
List node #5 has value 5

~list_t()
Deleting node with value 1
Deleting node with value 2
Deleting node with value 3
Deleting node with value 4
Deleting node with value 5
</code></pre>
<p>// edit:<br />
Reduziert aufs Wesentliche</p>
<pre><code class="language-cpp">template&lt; typename T &gt;
class list_t
{
	private:
		struct list_node_t
		{
			T value;
			list_node_t *next;

			list_node_t( T value ) : value( value ), next( nullptr ) {}
		} *root;

		list_t( list_t const &amp; ); // not copyable.

	public:
		list_t() : root( nullptr ) {}

		~list_t()
		{
			if( root ) {

				list_node_t *to_delete = root;
				list_node_t *next = nullptr;

				do {				
					next = to_delete-&gt;next;
					delete to_delete;

				} while( to_delete = next );
			}						
		}

		void push_back( int value )
		{
			if( !root ) {

				root = new list_node_t( value );

			} else {

				list_node_t *i = root;				
				for( ; i-&gt;next; i = i-&gt;next );
				i-&gt;next = new list_node_t( value );
			}
		}

	friend std::ostream&amp; operator&lt;&lt;( std::ostream &amp;os, list_t&lt; T &gt; const &amp;list )
	{
		std::size_t counter = 1;

		for( list_node_t *i = list.root; i; i = i-&gt;next, ++counter ) {

			os &lt;&lt; &quot;List node #&quot; &lt;&lt; counter &lt;&lt; &quot; has value &quot; &lt;&lt; i-&gt;value &lt;&lt; '\n';
		}

		return os;
	}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2260302</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260302</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sun, 14 Oct 2012 15:42:41 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Sun, 14 Oct 2012 17:08:33 GMT]]></title><description><![CDATA[<p>Vielen Dank für deine Antwort.<br />
Ich hab mir deine Kommentare bei meinen Quellcode mal angeschaut.<br />
Das mit den wtf hat mir nicht viel gesagt und ich weiß hat nicht wie ich die weglassen soll oder was daran falsch ist.<br />
Also ich glaub ich wollte so was wie doppelt verkettete Liste oder so machen also das ein Zeiger auf das vorige und einer auf das nächste Element zeigt.</p>
<p>Der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

struct Element{
	int data;
	Element *prev;
	Element *next;
};

class myList{
	private:
		Element *current;
		Element *before;

		Element *first;
		Element *last;

		bool    _New;
	public:
		myList(){

			    _New      = true;
				current   = nullptr;
				first     = nullptr;
				last      = nullptr;
				before	  = nullptr;
		}

		~myList(){								//Hier bin ich mir nicht sicher ob alles gelöscht wird.
			if(!(first == nullptr)){
				Element *loop       = first;
				Element *to_delete  = nullptr;

				while(!(loop-&gt;next == nullptr)){
					to_delete   = loop;
					loop        = to_delete-&gt;next;
					delete to_delete;
				}
			}
		}

		void push_back(int in){

				if(_New){
					current       = new Element;
					current-&gt;data = in;
					current-&gt;prev = nullptr;
					current-&gt;next = nullptr;
					first		  = current;
					last		  = current;
					before		  = current;
					_New		  = false;
				}else{
				current		  = new Element;

				before-&gt;next  = current;
				current-&gt;data = in;
				current-&gt;prev = before;
				current-&gt;next = nullptr;
				last		  = current;

				before        = current;
				}
		}

		void show(){

			Element *loop = first;

			if(!(loop == nullptr)){
				Element *loop = first;
				cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;		&quot; &lt;&lt; loop &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;prev 
					 &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl;

				while(!(loop-&gt;next == nullptr)){
					loop = loop-&gt;next;
					cout &lt;&lt; loop-&gt;data &lt;&lt; &quot;		&quot; &lt;&lt; loop &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;prev 
					 &lt;&lt; &quot;		&quot; &lt;&lt; loop-&gt;next &lt;&lt; endl &lt;&lt; endl;
				}
			}else{
				cout &lt;&lt; &quot;List empty!&quot; &lt;&lt; endl;
			}

	}
};

int main(){

	myList list;

	for(int i = 0; i &lt;= 10; i++){
	list.push_back(i);
	}

	list.show();

	myList list2;
	list2.show();
	list2.push_back(25155);
	list2.show();
	list.show();
	cin.get();
	return 0;
}
</code></pre>
<p>Noch so ne Frage nebenbei kann man das hier im Forum auch so machen das man ein Text(Quellcode) ein und ausklappen kann also auf ein Button drückt und dann wird der erst angezeigt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260335</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260335</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Sun, 14 Oct 2012 17:08:33 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Mon, 15 Oct 2012 10:12:49 GMT]]></title><description><![CDATA[<p>Wieso baust du dir überhaupt selbst eine verkettete Liste? Aufgabe für die Uni? Übungszwecke?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260606</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Mon, 15 Oct 2012 10:12:49 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Mon, 15 Oct 2012 10:24:33 GMT]]></title><description><![CDATA[<p>Ich mache das auch oft, da ich ungern etwas nutze was ich nicht selbst schon mal probiert habe(wenn es im Rahmen ist). Wenn wir alles nur noch fertige Libs und Engines nutzen, wer baut dann diese Libs/Engines? <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>Aber ich verstehe schon was du meinst, aber nur um Libs zu nutzen brauch man kein Studium zu machen. Außerdem sollte auch kein Informatiker nur Libs zusammen klatschen, dafür ist er einfach zu überqualifiziert. Diese Leute sollten z.B. eigenen Sprachen entwerfen, beraten, sehr große Projekte planen und nur ganz wenig selbst Hand anlegen. Ein Architekt käme auch nicht auf die Idee jetzt mit Zementkelle und Ziegelsteine hantieren zu wollen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260610</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260610</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Oct 2012 10:24:33 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Klassen Frage on Mon, 15 Oct 2012 11:49:38 GMT]]></title><description><![CDATA[<p>Ich mach das aus Übungszwecken um zu schauen was ich zum Thema Klassen und Pointer noch nicht kann...<br />
Also falls was ich zuletzt gepostet habe ohne Fehler ist.<br />
Würde ich mich freuen wenn mir jemand meine erste Frage beantwortet kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260665</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260665</guid><dc:creator><![CDATA[CraftPlorer]]></dc:creator><pubDate>Mon, 15 Oct 2012 11:49:38 GMT</pubDate></item></channel></rss>