Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure what answer the questioner was expecting, but here's some possible solutions:</p> <p><strong>Make 'A' a pointer:</strong></p> <pre><code>//Only takes 4 or 8 bytes (32 bit vs 64 bit code) for 'A', regardless of 'A's actual size, but must point to an 'A' located elsewhere in memory with the full size. class B { A *a; //Only 4 bytes. int b; }; </code></pre> <p><strong>Make 'A' static:</strong></p> <pre><code>//Doesn't assume any of 'A's size, but all instances of 'B' shares a single instance of 'A'. class B { static A a; int b; }; </code></pre> <p><strong>Pass 'A' into the functions of 'B':</strong></p> <pre><code>//Pass in the reference to 'a' when needed, so multiple 'B's can share the same 'a' explicitly when desired. class B { void q(A &amp;a) { this-&gt;b + a.a; return a.g(); } }; </code></pre> <p><strong>Make 'A' and 'B' not have virtual tables</strong> (probably the interviewer's point)</p> <pre><code>//By not having virtual functions, you save the (really small) cost of 4 bytes (8 bytes on 64 bit machines) class A { public: int f() { }; //Not virtual int a; } class B : public A { public: int g() { }; //Not virtual int b; } </code></pre> <p>It still costs you the size of A::a, and, unless you re-use 'a' in B instead of having B::b, you can't avoid those 4 bytes. And re-using one variable to mean something else entirely is a possible sign of really bad programming habits.</p> <p><strong>Unionize A'a and B's variables and put the functions in a single class</strong></p> <pre><code>class AB //Only 4 bytes total { public: union { int a; int b; }; void f(); void g(); }; </code></pre> <p>The bad idea about this is, you'll have to keep track of whether you should access 'a' or 'b', because both of them occupy the same 4 bytes of memory, and they can't both be using it at the same time.</p> <p>Another bad thing about this is that it is a sign that the class has too much responsibility. Is it an A or a B? If it's both, the all important question should be, "Why is it both?". It should have a <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow noreferrer">single-responsibility</a>, not be a <a href="https://stackoverflow.com/questions/3179396/what-does-monolithic-mean">monolith</a> of mixed purposes.</p> <p><strong>Make 'A' a template, and inherit from <code>A&lt;B&gt;</code>:</strong></p> <pre><code>template&lt;typename TypeB&gt; class A { int a; }; //Saves the cost of the virtual table... not that that really matters. class B : public A&lt;B&gt; { int b; }; </code></pre> <p>This last one is called the '<a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow noreferrer">curiously recurring template pattern</a>' (CRTP) The idea is that the inherited '<code>A&lt;B&gt;</code>' can call access variables and functions from 'B' (if you pass B's 'this' pointer into A's constructor), and 'B' can access variables and functions directly from '<code>A&lt;B&gt;</code>'.</p> <p>You're inheriting from a compile-time generated version of the template 'A', that is generated for 'B'.</p>
 

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