<?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[new und delete operatoren überladen]]></title><description><![CDATA[<p>Hallo NG,</p>
<p>ich möchte die Operatoren new und delete in der folgenden Klasse überladen.<br />
Die example.h</p>
<pre><code class="language-cpp">#ifndef OWNEXAMPLE_H
#define OWNEXAMPLE_H

#include &lt;cstdlib&gt;

namespace own {

class Example{
public:
    static void* operator new (std::size_t size) throw(std::bad_alloc);
    static void operator delete(void *a);

    explicit Example();

    virtual ~Example();

private:
  static unsigned int refCount;
};

}

#endif
</code></pre>
<p>Die example.cpp</p>
<pre><code class="language-cpp">#include &quot;example.h&quot;

namespace own {

unsigned int Example::refCount = 0;

Example::Example()
{
}

Example::~Example()
{
}

static void* Example::operator new (std::size_t size) throw(std::bad_alloc){

  void *a = malloc(size);

  if(NULL == a){
    throw (std::bad_alloc);
  }

  refCount++;

  return a;
  }

static void Example::operator delete (void *a){
  free(a);
}
}
</code></pre>
<p>Beim Compilieren kommt immer:<br />
example.h:10: error: expected type-specifier<br />
example.h:10: error: expected <code>)' example.h:10: error: expected ‘;’ example.cpp:16: error: expected type-specifier example.cpp:16: error: expected</code>)'<br />
example.cpp:16: error: expected initializer</p>
<p>Was mache ich den falsch?</p>
<p>Gruß und Danke schonmal,<br />
M.Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/174450/new-und-delete-operatoren-überladen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 18:54:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/174450.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Feb 2007 13:12:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:12:00 GMT]]></title><description><![CDATA[<p>Hallo NG,</p>
<p>ich möchte die Operatoren new und delete in der folgenden Klasse überladen.<br />
Die example.h</p>
<pre><code class="language-cpp">#ifndef OWNEXAMPLE_H
#define OWNEXAMPLE_H

#include &lt;cstdlib&gt;

namespace own {

class Example{
public:
    static void* operator new (std::size_t size) throw(std::bad_alloc);
    static void operator delete(void *a);

    explicit Example();

    virtual ~Example();

private:
  static unsigned int refCount;
};

}

#endif
</code></pre>
<p>Die example.cpp</p>
<pre><code class="language-cpp">#include &quot;example.h&quot;

namespace own {

unsigned int Example::refCount = 0;

Example::Example()
{
}

Example::~Example()
{
}

static void* Example::operator new (std::size_t size) throw(std::bad_alloc){

  void *a = malloc(size);

  if(NULL == a){
    throw (std::bad_alloc);
  }

  refCount++;

  return a;
  }

static void Example::operator delete (void *a){
  free(a);
}
}
</code></pre>
<p>Beim Compilieren kommt immer:<br />
example.h:10: error: expected type-specifier<br />
example.h:10: error: expected <code>)' example.h:10: error: expected ‘;’ example.cpp:16: error: expected type-specifier example.cpp:16: error: expected</code>)'<br />
example.cpp:16: error: expected initializer</p>
<p>Was mache ich den falsch?</p>
<p>Gruß und Danke schonmal,<br />
M.Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236179</guid><dc:creator><![CDATA[BlackPepper]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:12:00 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:19:47 GMT]]></title><description><![CDATA[<p>ich würde mal sagen, du benötigst ein &quot;#include &lt;new&gt;&quot;, damit der Compiler die bad_alloc findet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236185</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236185</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:19:47 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:33:54 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>ich würde mal sagen, du benötigst ein &quot;#include &lt;new&gt;&quot;, damit der Compiler die bad_alloc findet.</p>
</blockquote>
<p>Ok, das include ist drin. Aber nun meint er,</p>
<p>example.cpp:21: error: expected primary-expression before ‘)’ token<br />
????</p>
<p>Gruß,<br />
M. Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236192</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236192</guid><dc:creator><![CDATA[BlackPepper]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:33:54 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:44:14 GMT]]></title><description><![CDATA[<p>Schreibst du auch throw int; oder throw std::string; - Meinstens wirft man mit Objekten und nicht mit Typen um sich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236198</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236198</guid><dc:creator><![CDATA[Entenwickler wider da]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:44:14 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:44:17 GMT]]></title><description><![CDATA[<p>Ich kenne mich nicht so genau mit den Details aus, aber hat std::bad_alloc überhaupt einen Default-Ctor? Wenn nicht, müsstest du beim throw-Aufruf noch die richtigen Parameter mitgeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236199</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236199</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:44:17 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 13:49:02 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Ich kenne mich nicht so genau mit den Details aus, aber hat std::bad_alloc überhaupt einen Default-Ctor? Wenn nicht, müsstest du beim throw-Aufruf noch die richtigen Parameter mitgeben.</p>
</blockquote>
<p>Also in jeder Doku, die ich gefunden habe, steht einfach nur</p>
<pre><code class="language-cpp">throw(std::bad_alloc);
</code></pre>
<p>Ohne Parameter. Und ich mache ja nichts anderes.<br />
Ich habe z.B. <a href="http://de.wikipedia.org/wiki/%C3%9Cberladen" rel="nofollow">http://de.wikipedia.org/wiki/%C3%9Cberladen</a> herangezogen.</p>
<p>Gruß,<br />
M.Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236201</guid><dc:creator><![CDATA[BlackPepper]]></dc:creator><pubDate>Tue, 27 Feb 2007 13:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 14:02:55 GMT]]></title><description><![CDATA[<p>Außerdem müssen die Operatoren nicht-statische Methoden der Klasse sein. Desweiteren ist sowieso das 'static' bei der Methodenimplementierung zu viel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236206</guid><dc:creator><![CDATA[Th]]></dc:creator><pubDate>Tue, 27 Feb 2007 14:02:55 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 14:22:22 GMT]]></title><description><![CDATA[<p>die statics muessen weg und werfen musst du</p>
<pre><code class="language-cpp">throw std::bad_alloc();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1236218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236218</guid><dc:creator><![CDATA[rapso]]></dc:creator><pubDate>Tue, 27 Feb 2007 14:22:22 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 14:23:28 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Ich kenne mich nicht so genau mit den Details aus, aber hat std::bad_alloc überhaupt einen Default-Ctor? Wenn nicht, müsstest du beim throw-Aufruf noch die richtigen Parameter mitgeben.</p>
</blockquote>
<p>Danke, es muss:</p>
<pre><code class="language-cpp">std::bad_alloc a;
throw(a);
</code></pre>
<p>heißen. Zumindest kann ich Das compilieren. Auch wenn ich jetzt sagen würde, ich gebe ein lokales Objekt zurück. Aber</p>
<pre><code class="language-cpp">if(NULL == a){
    std::bad_alloc *a new bad_alloc();
    throw(*a);
  }
</code></pre>
<p>wird auch nicht compiliert. Das Objekt müßte dann aber der Empfänger der Exception freigeben, und daß empfangene Exceptions deleted werden müßten, wäre mir neu.</p>
<p>Gruß,<br />
M.Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236219</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236219</guid><dc:creator><![CDATA[BlackPepper]]></dc:creator><pubDate>Tue, 27 Feb 2007 14:23:28 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 14:26:44 GMT]]></title><description><![CDATA[<p>BlackPepper schrieb:</p>
<blockquote>
<p>Danke, es muss:</p>
<pre><code class="language-cpp">std::bad_alloc a;
throw(a);
</code></pre>
<p>heißen. Zumindest kann ich Das compilieren. Auch wenn ich jetzt sagen würde, ich gebe ein lokales Objekt zurück.</p>
</blockquote>
<p>Stimmt. Und das lokale Objekt wird kopiert, wie überall wo man mit blanken Objekten hantiert. Achja: Abkürzen kannst Du das ganze wie rapso, s.o.</p>
<blockquote>
<p>Aber</p>
<pre><code class="language-cpp">if(NULL == a){
    std::bad_alloc *a new bad_alloc();
    throw(*a);
  }
</code></pre>
<p>wird auch nicht compiliert. Das Objekt müßte dann aber der Empfänger der Exception freigeben, und daß empfangene Exceptions deleted werden müßten, wäre mir neu.</p>
</blockquote>
<p>Stimmt nicht ganz, da Du widerum ein Objekt zurückgibst. Das wird wie übrlich kopiert, und niemand <strong>kann</strong> die Exception mehr freigeben. Das von Dir geschilderte Problem würde auftauchen, wenn Du den Zeiger statt des Objekts wirfst (und natürlich auch einen Zeiger fängst).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236220</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236220</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 27 Feb 2007 14:26:44 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 14:47:37 GMT]]></title><description><![CDATA[<p>BlackPepper schrieb:</p>
<blockquote>
<p>...<br />
Also in jeder Doku, die ich gefunden habe, steht einfach nur</p>
<pre><code class="language-cpp">throw(std::bad_alloc);
</code></pre>
<p>...</p>
</blockquote>
<p>sicher, dass es sich dabei nicht um eine &quot;exception specification&quot; handelt (die syntaktisch etwas komplett Anderes ist) ?</p>
<pre><code class="language-cpp">// exception specification: Sagt aus, dass diese Funktion nur eine bad_alloc() werfen soll.
// --&gt; in der DEKLARATION der Funktion
void f() throw(std::bad_alloc)
{
    .... // irgendwas
    // das Werfen einer exception
    // --&gt; in der DEFINITION der Funktion
    throw std::bad_alloc();
    .... // irgendwas
}
</code></pre>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236226</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236226</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Tue, 27 Feb 2007 14:47:37 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 15:07:41 GMT]]></title><description><![CDATA[<p>Th schrieb:</p>
<blockquote>
<p>Außerdem müssen die Operatoren nicht-statische Methoden der Klasse sein. Desweiteren ist sowieso das 'static' bei der Methodenimplementierung zu viel.</p>
</blockquote>
<p>Das trifft auf operator new und operator delete gerade nicht zu. Diese Operatoren sind, wenn sie in einer Klasse überladen werden, immer statisch. Es ist nicht notwendig, sie explizit mit static zu deklarien, aber möglich. Es kann ja auch nicht anders sein, da beim Aufruf dieser Operatoren das Objekt noch nicht bzw. nicht mehr existiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236231</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 27 Feb 2007 15:07:41 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 15:15:36 GMT]]></title><description><![CDATA[<p>Im Übrigen entsprechen deine operatoren nicht dem üblichen Verhalten</p>
<pre><code class="language-cpp">#include &quot;example.h&quot;

namespace own {

unsigned int Example::refCount = 0;

Example::Example()
{
}

Example::~Example()
{
}

void* Example::operator new (std::size_t size) throw(std::bad_alloc)
{
// Anfragen nach 0 Byte werden als Anfragen auf 1 Byte betrachtet
  if( size == 0 ) {
    size = 1;              
  }
  while( true )
  {
// Versuche Speicher anzufordern
    void *a = malloc(size);       
// Wenn erfolgreich, gib ZEiger zurück
    if(NULL != a){           
      return a;                      
    }
//Schlug dieZuweisung fehl muss der new_handler aufgerufen werden
    new_handler globalHandler = set_new_handler( 0 );
    set_new_handler( globalHandler );
// Versuch das Problem zu lösen mit dem handler
    if ( globalHandler ) {     
      (*globalHandler)();
    }
// Wenn nicht, wirf exception
    else {
      throw (std::bad_alloc);    
    }
  }

void Example::operator delete (void *a) throw()        // delete darf nichts werfen
{
// Es ist immer sichern, den 0-Zeiger zu löschen
  if( a == 0 ) {
    return;   
  }
  free(a);
}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1236232</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236232</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Tue, 27 Feb 2007 15:15:36 GMT</pubDate></item><item><title><![CDATA[Reply to new und delete operatoren überladen on Tue, 27 Feb 2007 15:14:51 GMT]]></title><description><![CDATA[<p>rapso schrieb:</p>
<blockquote>
<p>die statics muessen weg und werfen musst du</p>
<pre><code class="language-cpp">throw std::bad_alloc();
</code></pre>
</blockquote>
<p>Juhu, jetzt tut alles.</p>
<p>Danke schön für die guten Tipps!</p>
<p>Gruß,<br />
M.Incani</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1236234</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1236234</guid><dc:creator><![CDATA[BlackPepper]]></dc:creator><pubDate>Tue, 27 Feb 2007 15:14:51 GMT</pubDate></item></channel></rss>