Note that there are some explanatory texts on larger screens.

plurals
  1. POA nice way to initialise template classes to a base class
    primarykey
    data
    text
    <p>I know this question may be very vague, and it's a bit of an extension to an answer I gave <a href="https://stackoverflow.com/questions/4225087/c-like-properties-in-native-c/4225302#4225302">here</a>. Basically, as a personal project I have been trying to replicate the "code-first" idiom, that is popular with C# programmers doing database-work, but in C++. Everything is great, and works well.</p> <p>One of the great things about C# is things such as attributes. We don't have that novelty in C++, so my code for a particular class that is then structured in the database, looks like this:</p> <pre><code>class User : public Record { public: User(Model&amp; m) : Record(L"_user", m) , Username(L"_userName", 32, false) , Nickname(L"_nickName", 32, false) { field(Username); field(Nickname); }; // eo ctor Field&lt;String&gt; Username; Field&lt;String&gt; Nickname; }; // eo class User </code></pre> <p>Ideally, I want to get rid of the <code>field</code> calls in the constructor. All they do, essentially, is register the field with the base class which then does the real work. How might I accomplish this kind of thing, and pass the details to the base class as simply as I have specified in the constructor?</p> <p>In a previous question that <a href="https://stackoverflow.com/questions/4179695/how-can-i-initialize-superclass-params-from-within-the-child-c-tor-in-c/4179711#4179711">I answered</a> I was able to give the base class information thanks to the way C++ constructs things. Is there a way I can leverage this in a constructor and get rid of the pesky <code>field</code> calls that may introduce programmer error?</p> <p>At this point I am happy that everything works wonderfully, but I'd love to cut down on clutter and have remove the need for this "registration" process in the constructors and somehow have them registered automatically with the base class.</p> <p>All opinions welcomed :)</p> <p><strong>EDIT</strong></p> <p>The implentation of <code>field</code> is thusly:</p> <pre><code> void Record::field(detail::field_base&amp; field) { field.owner_ = this; fields_.push_back(&amp;field); }; // eo field** </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload