<?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[#define böse - auch hier?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte mal eine Frage bezüglich defines... und zwar liest man ja immer wieder dass diese böse/schlecht/schlechter Stil etc. sind und bis jetzt hatte ich auch noch keinen Fall wo ich welche gebraucht hätte. Allerdings kann ich folgendes Problem sehr elegant mit einem define lösen, daher wollte ich mal fragen ob es in so einem Fall gut ist oder ob auch hier etwas dagegen spricht:</p>
<pre><code>// test.cpp
namespace {
	#define my_exception(message_) custom_exception(__FILE__, __LINE__, message_)

	class test::custom_exception : public std::exception
	{
		public:
			custom_exception(std::string const &amp;file_name_, size_t line_number_, std::string const &amp;message_) {
				std::stringstream ss;
				ss &lt;&lt; file_name_ &lt;&lt; &quot; &quot; &lt;&lt; line_number_ &lt;&lt; &quot; &quot; &lt;&lt; message_;
				message = ss.str();
			}

			virtual const char* what() const throw() {
				return message.c_str();
			}

		private:
			std::string message;
	};
}

void test::foo()
{
	throw my_exception(&quot;my error message&quot;);
}

// main.cpp
#include &quot;test.h&quot;
int _tmain(int argc, _TCHAR* argv[])
{
	try {
		test::foo();
	}
	catch (std::exception const &amp;e) {
		std::cerr &lt;&lt; e.what();
	}
}
</code></pre>
<p>Oder gibt es noch eine bessere Methode das zu erreichen (also Datei und Zeilennummer mit angeben) ohne defines zu verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325592/define-böse-auch-hier</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 03:12:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325592.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 May 2014 13:05:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to #define böse - auch hier? on Fri, 09 May 2014 13:05:55 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte mal eine Frage bezüglich defines... und zwar liest man ja immer wieder dass diese böse/schlecht/schlechter Stil etc. sind und bis jetzt hatte ich auch noch keinen Fall wo ich welche gebraucht hätte. Allerdings kann ich folgendes Problem sehr elegant mit einem define lösen, daher wollte ich mal fragen ob es in so einem Fall gut ist oder ob auch hier etwas dagegen spricht:</p>
<pre><code>// test.cpp
namespace {
	#define my_exception(message_) custom_exception(__FILE__, __LINE__, message_)

	class test::custom_exception : public std::exception
	{
		public:
			custom_exception(std::string const &amp;file_name_, size_t line_number_, std::string const &amp;message_) {
				std::stringstream ss;
				ss &lt;&lt; file_name_ &lt;&lt; &quot; &quot; &lt;&lt; line_number_ &lt;&lt; &quot; &quot; &lt;&lt; message_;
				message = ss.str();
			}

			virtual const char* what() const throw() {
				return message.c_str();
			}

		private:
			std::string message;
	};
}

void test::foo()
{
	throw my_exception(&quot;my error message&quot;);
}

// main.cpp
#include &quot;test.h&quot;
int _tmain(int argc, _TCHAR* argv[])
{
	try {
		test::foo();
	}
	catch (std::exception const &amp;e) {
		std::cerr &lt;&lt; e.what();
	}
}
</code></pre>
<p>Oder gibt es noch eine bessere Methode das zu erreichen (also Datei und Zeilennummer mit angeben) ohne defines zu verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398289</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Fri, 09 May 2014 13:05:55 GMT</pubDate></item><item><title><![CDATA[Reply to #define böse - auch hier? on Fri, 09 May 2014 13:48:24 GMT]]></title><description><![CDATA[<p>#defines sollte man nicht verwenden, da Macros die Fehlersuche in vielen Fällen erschweren und in manchen fällen führt die Verwendung von #defines sogar zu nicht intuitiv nachvollziehbaren Zuständen:</p>
<pre><code>#define get_max(a, b) a &gt; b ? a : b
int a, b;
...
int c = get_max(a++, b)
</code></pre>
<p>In deinem Fall halte ich ein Macro für in Ordnung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398292</guid><dc:creator><![CDATA[JulianH]]></dc:creator><pubDate>Fri, 09 May 2014 13:48:24 GMT</pubDate></item><item><title><![CDATA[Reply to #define böse - auch hier? on Fri, 09 May 2014 13:39:52 GMT]]></title><description><![CDATA[<p>Ist in Ordnung, nur würde ich noch versuchen, den Namen so zu gestalten, daß er als Makro erkennbar ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398294</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 09 May 2014 13:39:52 GMT</pubDate></item><item><title><![CDATA[Reply to #define böse - auch hier? on Fri, 09 May 2014 14:56:14 GMT]]></title><description><![CDATA[<p>Ok alles klar , danke für die Antworten <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398305</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398305</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Fri, 09 May 2014 14:56:14 GMT</pubDate></item></channel></rss>