News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ arrays tips - Pointers, arrays, and problems

Started by ben2ong2, October 06, 2006, 04:50:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: C++ has a very large hole concerning type safety regarding pointers
and arrays.


RESPONSE: Taken from class notes on Adv C++ by Desmond Dsouza

class A {
public:
   int av;
};

class B : public A {
public:
   int bv;
};

void f (A* x)
{
   x[1].av = 0;
}

main ()
{
   B y [100];
   y
  • .bv = 1;   

       f (y);

       // One of the B in the array has just been stomped on.
       // This assertion will fail.

       assert (y
  • .bv == 1);
    }


    Never write methods that accept (manipulate) arrays of objects of types
    that may be subclasses. Someday they will fail!

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login