Ryan's District Boards

Computer, programming, and webmaster help , support , tips and tricks => Tutorials Zone! => Internet webmaster computer programming technology tips and tricks => C++ / C / C# ....Tutorials => Topic started by: ben2ong2 on October 06, 2006, 05:55:09 PM

Title: c++ arrays tips - Very large hole int the type system for arrays and inheritance
Post by: ben2ong2 on October 06, 2006, 05:55:09 PM
[ Example adapted from "C++ Gotchas", tutorial notes by Tom
  Cargill, page 44. ]


class B {
  public:
     int x;
};

class D : public B {
  public:
     int y;
};


void foo (B a[])  // Note argument type
{
    for (int i = 0; i < 6; i++)
        printf ("%d %d\n", i, a.x);
}


main ()
{
    D d[6];

    for (int i = 0; i< 6; i++)
        d.x = i;

    f(d);

    return 0;
}

The output of this program (on some implementation) is shown below.
Moral of the story is that inheritance and arrays do not work
together well (there is little type safety).

0 0
1 536871025
2 1
3 7455272
4 2
5 -613566757