Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:07:53+00:00 2026-06-10T04:07:53+00:00

#include <iostream> class B { public: B () : b(bCounter++) {} int b; static

  • 0
#include <iostream> 
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<N; i++) 
         s+=arr[i].b; 
    return s; 
} 
int sum2 (D* arr) { 
     int s = 0; 
     for (int i=0; i<N; i++) s+=arr[i].b+arr[i].d; 
     return s; 
} 
int main() { 
    std::cout << sum1(arrB) << std::endl; 
    std::cout << sum1(arrD) << std::endl; 
    std::cout << sum2(arrD) << std::endl; 
    return 0; 
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:07:55+00:00Added an answer on June 10, 2026 at 4:07 am

    You have been unlucky enough to hit one of the sweet spots of the type system that allows to compile perfectly invalid code.

    The function int sum1 (B* arr) takes a pointer to a B object as argument according to the signature, but semantically it really takes a pointer to an array of B objects. When you call sum1(arrD) you are violating that contract by passing not an array of B objects, but rather an array of D objects. How do they differ? Pointer arithmetic is done based on the size of the type of the pointer, and a B object and a D object have different sizes.

    An array of D is not an array of B

    In general, a container of a derived type is not a container of the base type. If you think about it, the contract of a container of D is that it holds, well, D objects, but if a container of D was a container of B, then you would be able to add B objects (if the argument was extending, you might even consider adding D1 objects –also derived from B!).

    If instead of raw arrays you were using higher order constructs, like std::vector the compiler would have blocked you from passing a std::vector<D> in place of a std::vector<B>, but why did it not stop you in the case of an array?

    If an array of D is not an array of B, why did the program compile at all?

    The answer to this predates C++. In C, all arguments to functions are passed by value. Some people consider that you can also pass-by-pointer, but that is just passing a pointer by-value. But arrays are large, and it would be very expensive to pass arrays by value. At the same time, when you dynamically allocate memory you use pointers, although conceptually, when you malloc 10 ints you are allocating an array of int. The designers of the C language considered this and made an exception to the pass by value rules: if you try to pass an array by value, a pointer to the first element is obtained, and that pointer is passed instead of the array (a similar rule exists for functions, you cannot copy a function, so passing a function implicitly obtains a pointer to the function and passes that instead). The same rules have been in C++ since the beginning.

    Now, the next problem is that the type system does not differentiate from a pointer to an element when that is all there is, and a pointer to an element that is part of an array. And this has consequences. A pointer to a D object can be implicitly converted to a pointer to B, since B is a base of D, and the whole object of OO programming is being able to use derived types in place of base objects (well, that for the purpose of polymorphism).

    Now going back to your original code, when you write sum1( arrD ), arrD is used as an rvalue, and that means that the array decays to a pointer to the first element, so it effectively is translated to sum1( &arrD[0] ). The subexpression &arrD[0] is a pointer, and a pointer is just a pointer… sum1 takes a pointer to a B, and a pointer to D is implicitly convertible to a pointer to B, so the compiler gladly does that conversion for you: sum1( static_cast<B*>(&arrD[0]) ). If the function just took the pointer and used it as a single element, that would be fine, as you can pass a D in place of a B, but an array of D is not an array of B… even if the compiler allowed you to pass it as such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <boost/shared_ptr.hpp> using namespace std; class A { public: const shared_ptr<const int>
#include <iostream> using namespace std; class Imp { public: int X(int) {return 50;} int
#include <iostream> using namespace std; class B { public: int getMsg(int i) { return
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include <iostream> #include <cstring> #include <QString> using namespace std; class A { public: static
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
#include <iostream> #include <fstream> using namespace std; class binaryOperators { public: int i; binaryOperators
plugin1.cpp: #include <iostream> static class TestStatic { public: TestStatic() { std::cout << TestStatic create
#include <iostream> using namespace std; class X { public: X() { cout<<Cons<<endl; } X(const
#include <iostream> #include <vector> #include <cassert> class a_class { public: int num_IN; a_class():num_IN(0){} a_class(a_class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.