<?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[using base_class::base_class;]]></title><description><![CDATA[<p>hallo leute</p>
<p>hab grad folgendes versucht:</p>
<pre><code>class username : public std::string
{
   public:
      using std::string::string;
      static constexpr char *field_name = &quot;username&quot;;
}; 

std::string temp = &quot;test&quot;; 
username name1(temp);
</code></pre>
<p>fehlermeldung ist dann so:</p>
<pre><code>Konvertierung von Argument 1 von &quot;std::string&quot; in &quot;const std::allocator&lt;char&gt; &amp;&quot; nicht möglich
</code></pre>
<p>intellisense zeigt mir im VS 19 konstruktoren an, aber es ist keinen fuer std::string dabei. warum ist das so ?</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/338772/using-base_class-base_class</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 04:37:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/338772.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 09 Jul 2016 11:12:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 11:12:34 GMT]]></title><description><![CDATA[<p>hallo leute</p>
<p>hab grad folgendes versucht:</p>
<pre><code>class username : public std::string
{
   public:
      using std::string::string;
      static constexpr char *field_name = &quot;username&quot;;
}; 

std::string temp = &quot;test&quot;; 
username name1(temp);
</code></pre>
<p>fehlermeldung ist dann so:</p>
<pre><code>Konvertierung von Argument 1 von &quot;std::string&quot; in &quot;const std::allocator&lt;char&gt; &amp;&quot; nicht möglich
</code></pre>
<p>intellisense zeigt mir im VS 19 konstruktoren an, aber es ist keinen fuer std::string dabei. warum ist das so ?</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501697</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501697</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sat, 09 Jul 2016 11:12:34 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 11:32:56 GMT]]></title><description><![CDATA[<p>Der Kopierkonstruktor wird nicht ererbt - wäre auch doof.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501698</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sat, 09 Jul 2016 11:32:56 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 12:29:32 GMT]]></title><description><![CDATA[<p>waere aber genau das, was ich hier braeuchte</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501701</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501701</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sat, 09 Jul 2016 12:29:32 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 13:53:54 GMT]]></title><description><![CDATA[<p>sicher?</p>
<p>in deinem beispiel hast einen impliziten copy-ctor (zum kopieren)<br />
der copy-ctor von std::string dient der kopie von std::strings</p>
<p>du brauchst allerdings einen konstruktor, der std::strings zu usernames *konvertiert*. die alternative wäre ziemlich bald einmal slicing.</p>
<p>im übrigen denke ich, dass das problem schon ein bisschen früher anfängt. dieselbe logik betrifft nämlich das &quot;:public&quot; (ist-ein) - in wahrheit ist das eine &quot;ist-implementiert-als&quot;, also eine :private, beziehung (oder komposition). in C++17 könntest du wohl operator.() dafür verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501705</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501705</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sat, 09 Jul 2016 13:53:54 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 14:23:20 GMT]]></title><description><![CDATA[<p>ich brauche im grunde gesehen nur den datentyp string.<br />
ich baue viele URL parameter fuer webserver abfragen zusammen. hab da ca. 130 verschiedene parameter.<br />
nun koennte ich einfach ein std::pair<a href="std::string,std::string" rel="nofollow">std::string,std::string</a> nehmen und den namen und value des parameters reinschreiben und meinem stringstream zuweisen.<br />
ist aber fehleranfaellig.<br />
deshalb hab ich mir gedacht, das ich std::strings brauche die aber vom typ her trotzdem noch zu differenzieren sind und ich ihm auhc noch gleich den parameternamen als statischen cstring mitgeben kann</p>
<p>hab es jetzt mal so geloest:</p>
<pre><code>template&lt;class T&gt;
class string_class : public std::string
{
   public:
      using std::string::string;
      static constexpr char *field_name = T::field_name;

      string_class(void) { }
      string_class(const string_class &amp;s) : std::string(s) { }
      string_class(const std::string &amp;str) : std::string(str) { }

      auto operator=(const string_class &amp;s) -&gt; string_class&amp; { std::string::operator=(s); return *this; }
      auto operator=(const std::string &amp;s) -&gt; string_class&amp; { std::string::operator=(s); return *this; }
}; /* class string_class */

struct account_traits { static constexpr char *field_name = &quot;account&quot;; };
using account = string_class&lt;account_traits&gt;;

template&lt;class T&gt;
auto operator&lt;&lt;(std::ostream &amp;out, const string_class&lt;T&gt; &amp;s) -&gt; std::ostream&amp;
{
   out &lt;&lt; T::field_name &lt;&lt; '=' &lt;&lt; static_cast&lt;const std::string&amp;&gt;(s);

   return out;
}

...

account my_account(&quot;test&quot;);

std::cout &lt;&lt; my_account;
</code></pre>
<p>und er schreibt mir brav in den stream</p>
<pre><code>account=test
</code></pre>
<p>klar haette ich dafuer auch eine funktion machen koennen:</p>
<pre><code>print_account(std::ostream &amp;out, const std::string &amp;acc) -&gt; void
{ out &lt;&lt; &quot;account=&quot; &lt;&lt; acc; }
</code></pre>
<p>aber das gefaellt mir weniger.</p>
<p>ich mag data-driven APIs sowieso nicht und sie erleichtern das programieren auch nicht wirklich. es schaut immer alles so zusammengefrickelt aus, mit den strings dazwischen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501706</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501706</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sat, 09 Jul 2016 14:23:20 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 15:41:31 GMT]]></title><description><![CDATA[<p>ziemlich sicher ist das nicht, was du willst - private vererbung wäre besser.</p>
<pre><code class="language-cpp">account acc = &quot;foo&quot;;

cout &lt;&lt; acc + &quot;bar&quot; &lt;&lt; endl;
//ausgabe: foobar, nicht account=foobar
</code></pre>
<pre><code class="language-cpp">template&lt;class T&gt; 
class string_class : private std::string 
{ 
   public: 
      using std::string::string; 
      static constexpr const char *field_name = T::field_name; //const über constexpr ist char*const, nicht const char*

      string_class() = default; 

    template &lt;class Other&gt;
      string_class(Other const&amp;s) : std::string{s} { } 
template &lt;class Other&gt;
      string_class(Other&amp;&amp; s) : std::string{std::move(s)} { } 

template&lt;class S&gt; 
friend auto operator&lt;&lt;(std::ostream &amp;out, const string_class&lt;S&gt; &amp;s) -&gt; std::ostream&amp; 
{ 
   out &lt;&lt; T::field_name &lt;&lt; '=' &lt;&lt; static_cast&lt;std::string const&amp;&gt;(s); 

   return out; 
} 

}; 

struct account_traits { static constexpr const char *  field_name  = &quot;account&quot;; }; 
using account = string_class&lt;account_traits&gt;;
</code></pre>
<p>oder überhaupt etwas anderes? müsste man überlegen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501711</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sat, 09 Jul 2016 15:41:31 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 18:03:51 GMT]]></title><description><![CDATA[<p>beachte die klassen in der STL sind nicht für Vererbung geeignet/gedacht (fehlender virtueller Destructor)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501714</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501714</guid><dc:creator><![CDATA[firefly]]></dc:creator><pubDate>Sat, 09 Jul 2016 18:03:51 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 21:06:04 GMT]]></title><description><![CDATA[<p>firefly schrieb:</p>
<blockquote>
<p>beachte die klassen in der STL sind nicht für Vererbung geeignet/gedacht (fehlender virtueller Destructor)</p>
</blockquote>
<p>ja ich weiß. aber ich erweitere die klasse ja nicht. insofern sollte das kein problem sein.</p>
<p>@dove<br />
ich moechte mit string_class&lt;T&gt; genauso arbeiten koennen wie mit einem normalen std::string. deshalb erbe ich public.</p>
<p>ist <code>constexpr char *field_name = &quot;name&quot;</code> nicht standard konform ?<br />
VC2015 frisst es mit Warnstufe 4 problemlos.</p>
<p>dein beispiel mit der string-addition ist mir schon klar. aber die string_class verwende ich nur intern um mir das leben etwas einfacher zu machen.<br />
die string_classen sind in einem parameterpack zusammengefasst, das auch das streaming uebernimmt. da sehe ich keine probleme, da ich direkt nicht damit arbeite.<br />
einzeln schreibe ich die nie in einen stream.</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501726</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sat, 09 Jul 2016 21:06:04 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sat, 09 Jul 2016 21:58:57 GMT]]></title><description><![CDATA[<p>nur ein aside, das c-literal via char* ist seit C++11 nicht mehr standardkonform, davor deprecated, was vielleicht erklärt, warum VC das immer noch erlaubt.</p>
<p>wenn du slicing garantiert ausschließen kannst, spricht imo wohl nichts dagegen (außer prinzipielle gründe à la ist-ein vs ist-implementiert-als). dafür musst du halt selbst einen passenden copy-ctor schreiben.</p>
<p>dir kann halt manchmal dann passieren, dass eine deiner string_class-instanzen zu einem string gemacht wird und das mit username=string dann nicht mehr funktioniert. das aneinanderreihen von mehreren solchen x=y mittels &amp; stell ich mir auch nicht so flexibel vor.</p>
<p>wie auch immer, je nach compiler-status kannst du dich hier auch mit (noch mehr) constexpr und non-type-template parametern spielen:</p>
<pre><code class="language-cpp">template &lt;char const* name&gt;
struct Parameter : public string { 
    using string::string;
    Parameter(string const&amp; str) : string(str) {}
    Parameter(Parameter const&amp;) = default;
    Parameter() = default;

};

template &lt;char const* name&gt;
ostream&amp; operator&lt;&lt; (ostream&amp; out, Parameter&lt;name&gt; const&amp; arg) {
    return out &lt;&lt; name &lt;&lt; &quot;=&quot; &lt;&lt; static_cast&lt;string const&amp;&gt;(arg); 
}

constexpr char const username_id[] = &quot;username&quot;;
using username = Parameter&lt;username_id&gt;;
</code></pre>
<p>dann noch private vererbung und eventuell so etwas:</p>
<pre><code class="language-cpp">template &lt;char const* name&gt;
struct Parameter : private string { 
    using string::string;
    operator string&amp; () { return name + *this; }
//...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2501732</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501732</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sat, 09 Jul 2016 21:58:57 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 01:09:58 GMT]]></title><description><![CDATA[<p>Warum keine stinknormale Aggregation? Warum überhaupt erben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501734</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Sun, 10 Jul 2016 01:09:58 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 06:20:04 GMT]]></title><description><![CDATA[<p>5cript schrieb:</p>
<blockquote>
<p>Warum keine stinknormale Aggregation? Warum überhaupt erben?</p>
</blockquote>
<p>warum eine aggregation ? was soll mir das in dem fall bringen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501738</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sun, 10 Jul 2016 06:20:04 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 06:28:17 GMT]]></title><description><![CDATA[<p>dov schrieb:</p>
<blockquote>
<p>dann noch private vererbung und eventuell so etwas:</p>
<pre><code>template &lt;char const* name&gt;
struct Parameter : private string {
    using string::string;
    operator string&amp; () { return name + *this; }
//...
</code></pre>
</blockquote>
<p>du meintest</p>
<pre><code>operator string () { return name + *this; }
</code></pre>
<p>?</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501739</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sun, 10 Jul 2016 06:28:17 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 08:08:19 GMT]]></title><description><![CDATA[<p><a href="https://www.c-plusplus.net/forum/75672-full">https://www.c-plusplus.net/forum/75672-full</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501746</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501746</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 10 Jul 2016 08:08:19 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 12:44:38 GMT]]></title><description><![CDATA[<p><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="😃"
    /> an volkard</p>
<p>und ja natürlich hätte ich operator string() schreiben sollen, man zeigt um 23:58 allerdings doch schon recht ausgeprägte ermüdungserscheinungen.</p>
<p>@Meep Meep: deine ursprüngliche frage ist ja beantwortet, oder? eventuell kann man sich aber noch ein ganz alternatives design überlegen, wenn wir mehr über den einsatzbereich wüssten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501768</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sun, 10 Jul 2016 12:44:38 GMT</pubDate></item><item><title><![CDATA[Reply to using base_class::base_class; on Sun, 10 Jul 2016 16:44:20 GMT]]></title><description><![CDATA[<p>Meep Meep schrieb:</p>
<blockquote>
<p>5cript schrieb:</p>
<blockquote>
<p>Warum keine stinknormale Aggregation? Warum überhaupt erben?</p>
</blockquote>
<p>warum eine aggregation ? was soll mir das in dem fall bringen ?</p>
</blockquote>
<p>Ich wollte damit sagen, dass mir deine Ansatz nicht gefällt überhaupt irgendwie von string zu erben, damit sich deine klasse wie ein string einsetzen lässt.<br />
Dann lieber den namen aggregieren und spezielle memberfunktionen und Konvertierungsoperator selbst schreiben und ggf operator&lt;&lt; überladen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501790</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Sun, 10 Jul 2016 16:44:20 GMT</pubDate></item></channel></rss>