<?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[Diese klasse so in ordnung?Tipps und Hinweise!]]></title><description><![CDATA[<pre><code class="language-cpp">//---------------------------------------------------------------------------

#pragma hdrstop
#include &lt;iostream&gt;
#include &lt;conio&gt;
#include &lt;string&gt;
#include &lt;strstream&gt;
using namespace std;

//---------------------------------------------------------------------------

#pragma argsused
class IntFeld
{
 public:
    IntFeld(int size = 0, int initDigit = 0);
    IntFeld(const IntFeld&amp; Feld);
    ~IntFeld();
    IntFeld&amp; operator=(const IntFeld&amp; Feld);

    int top()const;
    int first()const;
    int at(int index);
    void pushBack(int digit);
    void pop();
    int getSize()const;

    int operator[](int index);
    friend ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const IntFeld&amp; Feld);
    friend istream&amp; operator&gt;&gt;(istream&amp; istr,const IntFeld&amp; Feld);

    string GetLastError();

    bool isEqual(const IntFeld&amp; Feld);
    bool operator==(const IntFeld&amp; Feld);

 private:
    int         m_index;
    int*        m_data;
    int         m_size;
    string      m_errormsg;

};

//--------------------------------------------------------------------------

IntFeld::IntFeld(int size, int initDigit)
{
 m_data = new int[size];
 for(int i = 0; i &lt; size; ++i)
  m_data[i] = initDigit;

 m_size = size;
 m_index = -1;
}

//--------------------------------------------------------------------------

IntFeld::IntFeld(const IntFeld&amp; Feld)
{
 m_data = new int[Feld.m_size];
 m_size = Feld.m_size;
 m_index = Feld.m_index;

 for(int i = 0; i &lt; m_size; ++i)
  m_data[i] = Feld.m_data[i];
}

IntFeld::~IntFeld()
{
 delete[] m_data;
 m_data = 0;
 m_index = -1;
 m_size = 0;
}

//--------------------------------------------------------------------------

IntFeld&amp; IntFeld::operator=(const IntFeld&amp; Feld)
{
 if(this!=&amp;Feld)
    {
     delete[] m_data;
     m_size = 0;
     m_index = -1;
     m_size = Feld.m_size;

     m_data = new int[m_size];
     for(int i = 0; i &lt; m_size; ++i)
        {
         m_data[i] = Feld.m_data[i];
        }
     m_index = Feld.m_index;
    }
 return *this;
}

//--------------------------------------------------------------------------

int IntFeld::top()const
{
 return m_data[m_index];
}

//--------------------------------------------------------------------------

int IntFeld::first()const
{
 if(m_data)
    {
     if(m_index&gt;-1)
      return m_data[0];
    }
 return 0;
}

//--------------------------------------------------------------------------

int IntFeld::at(int index)
{
  if(index&gt;m_index || index&lt; -1)
   {
   string indexer;
   strstream str;
   str&lt;&lt;index;
   str&gt;&gt;indexer;
   str.str();

   m_errormsg = &quot;Index &quot;;
   m_errormsg += indexer;
   m_errormsg += &quot; out of range&quot;;
   throw std::out_of_range(m_errormsg.c_str());
   }
  else
   return m_data[index];

}

//--------------------------------------------------------------------------

void IntFeld::pushBack(int digit)
{
 m_index = m_index+1;
 //wenn größe des Arrays noch reicht
 if(m_index &lt;= m_size)
    {
     m_data[m_index] = digit;
    }
 else   //wenn array zu klein
    {
     //Altes Array in tempo kopieren;
     int* temp = new int[m_index];
     for(int i = 0; i &lt; m_index+1; ++i)
        temp[i] = m_data[i];

     delete[] m_data;
     m_size = m_index + 10;

     //neues array allokoieren und füllen
     m_data = new int[m_size];

     for(int i = 0; i &lt; m_index+1; ++i)
      m_data[i] = temp[i];

     m_data[m_index] = digit;
     delete[] temp;
    }
}

//--------------------------------------------------------------------------

void IntFeld::pop()
{
 if(m_data &amp;&amp; m_index&gt;-1)
    {
     m_data[m_index] = 0;
     m_index-=1;
    }
 else
   {
   m_errormsg = &quot;Kontainer nicht initialisiert oder index out of range&quot;;
   throw m_errormsg.c_str();
   }
}

//--------------------------------------------------------------------------

int IntFeld::getSize()const
{
 return m_size;
}

//--------------------------------------------------------------------------

int IntFeld::operator[](int index)
{
  if(index &gt; m_index)
   return 0;
  else
   return m_data[index];
}

//--------------------------------------------------------------------------

ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const IntFeld&amp; Feld)
{
 for(int i = 0; i &lt; Feld.m_index+1; ++i)
  ostr&lt;&lt;&quot;Element &quot;&lt;&lt;i&lt;&lt;&quot;: &quot;&lt;&lt;Feld.m_data[i]&lt;&lt;endl;

 return ostr;
}

//--------------------------------------------------------------------------

string IntFeld::GetLastError()
{
 return m_errormsg;
}

//--------------------------------------------------------------------------

bool IntFeld::isEqual(const IntFeld&amp; Feld)
{
 if(m_size==Feld.m_size)
    {
     for(int i = 0; i &lt; m_size; ++i)
        {
         if(m_data[i]!=Feld.m_data[i])
          return false;

        }
     return true;
    }
  else
   return false;
}

//--------------------------------------------------------------------------

bool IntFeld::operator==(const IntFeld&amp; Feld)
{
 return isEqual(Feld);
}

//--------------------------------------------------------------------------

}
//---------------------------------------------------------------------------
</code></pre>
<p>was sagt ihr zu dieser klasse?<br />
was habe ich falsch gemacht?<br />
was könnte ich besser machen?</p>
<p>gibt es speicherlecks?</p>
<p>Das ist eine übung und bringt keine fehler, möchte nur eure meinung und tipps hören!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/170667/diese-klasse-so-in-ordnung-tipps-und-hinweise</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 03:33:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/170667.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 18 Jan 2007 12:45:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Diese klasse so in ordnung?Tipps und Hinweise! on Thu, 18 Jan 2007 12:45:14 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">//---------------------------------------------------------------------------

#pragma hdrstop
#include &lt;iostream&gt;
#include &lt;conio&gt;
#include &lt;string&gt;
#include &lt;strstream&gt;
using namespace std;

//---------------------------------------------------------------------------

#pragma argsused
class IntFeld
{
 public:
    IntFeld(int size = 0, int initDigit = 0);
    IntFeld(const IntFeld&amp; Feld);
    ~IntFeld();
    IntFeld&amp; operator=(const IntFeld&amp; Feld);

    int top()const;
    int first()const;
    int at(int index);
    void pushBack(int digit);
    void pop();
    int getSize()const;

    int operator[](int index);
    friend ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const IntFeld&amp; Feld);
    friend istream&amp; operator&gt;&gt;(istream&amp; istr,const IntFeld&amp; Feld);

    string GetLastError();

    bool isEqual(const IntFeld&amp; Feld);
    bool operator==(const IntFeld&amp; Feld);

 private:
    int         m_index;
    int*        m_data;
    int         m_size;
    string      m_errormsg;

};

//--------------------------------------------------------------------------

IntFeld::IntFeld(int size, int initDigit)
{
 m_data = new int[size];
 for(int i = 0; i &lt; size; ++i)
  m_data[i] = initDigit;

 m_size = size;
 m_index = -1;
}

//--------------------------------------------------------------------------

IntFeld::IntFeld(const IntFeld&amp; Feld)
{
 m_data = new int[Feld.m_size];
 m_size = Feld.m_size;
 m_index = Feld.m_index;

 for(int i = 0; i &lt; m_size; ++i)
  m_data[i] = Feld.m_data[i];
}

IntFeld::~IntFeld()
{
 delete[] m_data;
 m_data = 0;
 m_index = -1;
 m_size = 0;
}

//--------------------------------------------------------------------------

IntFeld&amp; IntFeld::operator=(const IntFeld&amp; Feld)
{
 if(this!=&amp;Feld)
    {
     delete[] m_data;
     m_size = 0;
     m_index = -1;
     m_size = Feld.m_size;

     m_data = new int[m_size];
     for(int i = 0; i &lt; m_size; ++i)
        {
         m_data[i] = Feld.m_data[i];
        }
     m_index = Feld.m_index;
    }
 return *this;
}

//--------------------------------------------------------------------------

int IntFeld::top()const
{
 return m_data[m_index];
}

//--------------------------------------------------------------------------

int IntFeld::first()const
{
 if(m_data)
    {
     if(m_index&gt;-1)
      return m_data[0];
    }
 return 0;
}

//--------------------------------------------------------------------------

int IntFeld::at(int index)
{
  if(index&gt;m_index || index&lt; -1)
   {
   string indexer;
   strstream str;
   str&lt;&lt;index;
   str&gt;&gt;indexer;
   str.str();

   m_errormsg = &quot;Index &quot;;
   m_errormsg += indexer;
   m_errormsg += &quot; out of range&quot;;
   throw std::out_of_range(m_errormsg.c_str());
   }
  else
   return m_data[index];

}

//--------------------------------------------------------------------------

void IntFeld::pushBack(int digit)
{
 m_index = m_index+1;
 //wenn größe des Arrays noch reicht
 if(m_index &lt;= m_size)
    {
     m_data[m_index] = digit;
    }
 else   //wenn array zu klein
    {
     //Altes Array in tempo kopieren;
     int* temp = new int[m_index];
     for(int i = 0; i &lt; m_index+1; ++i)
        temp[i] = m_data[i];

     delete[] m_data;
     m_size = m_index + 10;

     //neues array allokoieren und füllen
     m_data = new int[m_size];

     for(int i = 0; i &lt; m_index+1; ++i)
      m_data[i] = temp[i];

     m_data[m_index] = digit;
     delete[] temp;
    }
}

//--------------------------------------------------------------------------

void IntFeld::pop()
{
 if(m_data &amp;&amp; m_index&gt;-1)
    {
     m_data[m_index] = 0;
     m_index-=1;
    }
 else
   {
   m_errormsg = &quot;Kontainer nicht initialisiert oder index out of range&quot;;
   throw m_errormsg.c_str();
   }
}

//--------------------------------------------------------------------------

int IntFeld::getSize()const
{
 return m_size;
}

//--------------------------------------------------------------------------

int IntFeld::operator[](int index)
{
  if(index &gt; m_index)
   return 0;
  else
   return m_data[index];
}

//--------------------------------------------------------------------------

ostream&amp; operator&lt;&lt;(ostream&amp; ostr,const IntFeld&amp; Feld)
{
 for(int i = 0; i &lt; Feld.m_index+1; ++i)
  ostr&lt;&lt;&quot;Element &quot;&lt;&lt;i&lt;&lt;&quot;: &quot;&lt;&lt;Feld.m_data[i]&lt;&lt;endl;

 return ostr;
}

//--------------------------------------------------------------------------

string IntFeld::GetLastError()
{
 return m_errormsg;
}

//--------------------------------------------------------------------------

bool IntFeld::isEqual(const IntFeld&amp; Feld)
{
 if(m_size==Feld.m_size)
    {
     for(int i = 0; i &lt; m_size; ++i)
        {
         if(m_data[i]!=Feld.m_data[i])
          return false;

        }
     return true;
    }
  else
   return false;
}

//--------------------------------------------------------------------------

bool IntFeld::operator==(const IntFeld&amp; Feld)
{
 return isEqual(Feld);
}

//--------------------------------------------------------------------------

}
//---------------------------------------------------------------------------
</code></pre>
<p>was sagt ihr zu dieser klasse?<br />
was habe ich falsch gemacht?<br />
was könnte ich besser machen?</p>
<p>gibt es speicherlecks?</p>
<p>Das ist eine übung und bringt keine fehler, möchte nur eure meinung und tipps hören!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1211986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1211986</guid><dc:creator><![CDATA[DATO(S)]]></dc:creator><pubDate>Thu, 18 Jan 2007 12:45:14 GMT</pubDate></item><item><title><![CDATA[Reply to Diese klasse so in ordnung?Tipps und Hinweise! on Thu, 18 Jan 2007 12:55:53 GMT]]></title><description><![CDATA[<p>Du solltest versuchen, diesen pragma-&quot;müll&quot; nicht zu verwenden, solange du es nicht wirklich nötig hast <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="😉"
    /> dann hast du noch nicht überall wo möglich const verwendet. Ich meine damit zum Beispiel bei operator== und so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1211997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1211997</guid><dc:creator><![CDATA[Antwort]]></dc:creator><pubDate>Thu, 18 Jan 2007 12:55:53 GMT</pubDate></item><item><title><![CDATA[Reply to Diese klasse so in ordnung?Tipps und Hinweise! on Thu, 18 Jan 2007 12:56:43 GMT]]></title><description><![CDATA[<p>Auf Anhieb sieht die Klasse schon recht gut aus, allerdings:</p>
<ul>
<li>Die Speicherverwaltung wird auf Dauer etwas zeitaufwändig, wenn du nacheinander viele Elemente einfügst<br />
(std::vector&lt;&gt; verdoppelt die Kapazität, wenn er voll ist)</li>
<li>du hast keine Methoden, um bestehende Elemente der Liste ändern zu können<br />
(at() und op[] brauchst du in zwei Versionen - <code>int at(int index) const;</code> (lesender Zugriff bei konstantem Feld) und <code>int&amp; at(int index);</code> (freier Zugriff bei nicht-konstantem Feld))</li>
<li>du wirfst Zeiger auf Zeichenketten, die im Rahmen des Stack Unwinding vor dem catch() entsorgt wurden, also bekommt der Aufrufer im Fehlerfall nur Müll<br />
(wirf lieber ein std::runtime_error-Objekt (oder eine davon abgeleitete Klasse))</li>
</ul>
<p>PS: Was für einen Grund hat es, das selber zu schreiben. Was du dort hast, kannst du alles mit std::vector&lt;int&gt; erledigen (eventuell außer op&lt;&lt; und op&gt;&gt;).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1211999</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1211999</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 18 Jan 2007 12:56:43 GMT</pubDate></item><item><title><![CDATA[Reply to Diese klasse so in ordnung?Tipps und Hinweise! on Thu, 18 Jan 2007 13:33:35 GMT]]></title><description><![CDATA[<p>Hmmm, ist alles nicht ganz Exception-Sicher. Versuche in den Funktion in denen die Daten verändert werden erstmals diese Veränderung zB temporär zu machen und nachher diese dann erst übernehmen. ZB bei op= erstmal ein temporäres IntFeld Objekt erzeugen und nachher swappen ...</p>
<p>Vielleicht mache ich das heute Abend mal <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="😉"
    /><br />
Am besten in Exceptional C++ die Kaptitel über Exceptions lesen ( einfach traumhaft ) ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1212026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1212026</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 18 Jan 2007 13:33:35 GMT</pubDate></item></channel></rss>