Assigning derived class array to base class pointer
<pre><code>#include &lt;iostream&gt; 
class B { 
public: 
 B () : b(bCounter++) {} 
int b; 
static int bCounter;  
}; 
int B::bCounter = 0; 
class D : public B { 
public: 
D () : d(bCounter) {} 
int d; 
}; 
const int N = 10; 
B arrB[N]; 
D arrD[N]; 
int sum1 (B* arr) { 
    int s = 0; 
    for (int i=0; i&lt;N; i++) 
         s+=arr[i].b; 
    return s; 
} 
int sum2 (D* arr) { 
     int s = 0; 
     for (int i=0; i&lt;N; i++) s+=arr[i].b+arr[i].d; 
     return s; 
} 
int main() { 
    std::cout &lt;&lt; sum1(arrB) &lt;&lt; std::endl; 
    std::cout &lt;&lt; sum1(arrD) &lt;&lt; std::endl; 
    std::cout &lt;&lt; sum2(arrD) &lt;&lt; std::endl; 
    return 0; 
}
</code></pre>

<p>The problem is in line 2 of main function. I expected that when sum1() function was called with argument arrD( which is an array of the Derived class objects), it would simply "cut off" the D::d, but in this case it rearranges the order in arrD, and the summing goes like this:
10+11+11+12+12+13+13+14+14+15
It seems to be alternating between b and d fields of arrD[i], and it should be summing up only b fields.
Can someone please explain why?
Thanks in advance.</p>
