Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>PREAMBLE:</h2> <p>While my answer is still Ok, I find <a href="https://stackoverflow.com/questions/112085/is-this-c-structure-initialization-trick-safe/1315024#1315024">litb's answer</a> quite superior to mine because:</p> <ol> <li>It teaches me a trick that I did not know (litb's answers usually have this effect, but this is the first time I write it down)</li> <li>It answers exactly the question (that is, initializing the original struct's part to zero)</li> </ol> <p>So please, consider litb's answer before mine. In fact, I suggest the question's author to consider litb's answer as the right one.</p> <h2>Original answer</h2> <p>Putting a true object (i.e. std::string) etc. inside will break, because the true object will be initialized before the memset, and then, overwritten by zeroes.</p> <p>Using the initialization list doesn't work for g++ (I'm surprised...). Initialize it instead in the CMyStruct constructor body. It will be C++ friendly:</p> <pre><code>class CMyStruct : public MY_STRUCT { public: CMyStruct() { n1 = 0 ; n2 = 0 ; } }; </code></pre> <p>P.S.: I assumed you did have <strong>no</strong> control over MY_STRUCT, of course. With control, you would have added the constructor directly inside MY_STRUCT and forgotten about inheritance. Note that you can add non-virtual methods to a C-like struct, and still have it behave as a struct.</p> <p>EDIT: Added missing parenthesis, after Lou Franco's comment. Thanks!</p> <p>EDIT 2 : I tried the code on g++, and for some reason, using the initialization list does not work. I corrected the code using the body constructor. The solution is still valid, though.</p> <p>Please reevaluate my post, as the original code was changed (see changelog for more info).</p> <p>EDIT 3 : After reading Rob's comment, I guess he has a point worthy of discussion: "Agreed, but this could be an enormous Win32 structure which may change with a new SDK, so a memset is future proof."</p> <p>I disagree: Knowing Microsoft, it won't change because of their need for perfect backward compatibility. They will create instead an extended MY_STRUCT<b>Ex</b> struct with the same initial layout as MY_STRUCT, with additionnal members at the end, and recognizable through a "size" member variable like the struct used for a RegisterWindow, IIRC.</p> <p>So the only valid point remaining from Rob's comment is the "enormous" struct. In this case, perhaps a memset is more convenient, but you will have to make MY_STRUCT a variable member of CMyStruct instead of inheriting from it.</p> <p>I see another hack, but I guess this would break because of possible struct alignment problem.</p> <p>EDIT 4: Please take a look at Frank Krueger's solution. I can't promise it's portable (I guess it is), but it is still interesting from a technical viewpoint because it shows one case where, in C++, the "this" pointer "address" moves from its base class to its inherited class.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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