8
class Base1
{
    private:
     int testInput; 
    public:
       Base1();
       virtual int GetRow(void) = 0;
 };

 Base1::Base1()
 {
   testInput = 0;
 }

class table : public Base1
{
   private:
    int row;    
   public:  
     table();
     virtual int GetRow(void);
};

table::table()
{   
  //Contructor
  row = 5;
}

int table::GetRow()
{
  return row;
}

int main ()
{
  Base1* pBase = new table[3];
  pBase[0].GetRow();
  pBase[1].GetRow();   //when i get to  this line, the compiler keep saying access
                           // violation.
  pBase[2].GetRow();

  return 0;
}

I'm trying to create an array of 3 table class. The requirement is I have to use Base object to do that.

Base1 * pBase = new table[3];  

look fine to me. But when I tried to access each table, the compiler said it's access violation. I don't know what wrong with this code. I'm using Visual Studio 2010 though.

Aesthete
  • 18,622
  • 6
  • 36
  • 45
Kaleidoscope
  • 83
  • 1
  • 1
  • 4

4 Answers4

18

In C++, polymorphism and arrays don't mix.

Since in general the size of the derived class is different to the size of the base class, polymorphism and pointer arithmetic don't play together nicely. Since array access involves pointer arithmetic, expressions such as pBase[1] don't work as expected.

One possibility is to have an array of pointers to your objects, perhaps even smart pointers to simplify memory management. But don't forget to define a virtual destructor in Base1.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
6

You're getting the error because the array is statically typed to Base1. That means that this line:

pBase[1].GetRow();

adds the size of Base1 in bytes to pBase and interprets this as the beginning of another Base1 object, but this actually points someplace into the middle of the first table instance.

If you need an array of polymorphic instances, you must store them in the array (or preferably in a std::vector) via pointers (or preferably some form of smart pointers).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
5

Agnew's response was spot on. Let me explain a little more. By augmenting your code, I print out the size of a Base1 and a table object as well as the addresses of the three table objects as they are created by the new operator:

A Base1 object is 8 bytes
A table object is 12 bytes
A table object is being constructed at 0x002977C0
A table object is being constructed at 0x002977CC
A table object is being constructed at 0x002977D8

As you can see those objects are spaced 12 bytes apart from each other in memory.

Now, let's print out the addresses that pBase[0], pBase[1] and pBase[2] give:

pBase[0] is at 0x002977C0
pBase[1] is at 0x002977C8
pBase[2] is at 0x002977D0

Now look at what happens: the pointers we get back are spaced 8 bytes apart. This is because the pointer arithmetic is done on an pointer whose type is Base1 and since Base1 is 8 bytes longs what the compiler does is to translate pBase[n] into pBase + (n * sizeof(Base1)).

Now you should be able to understand exactly why the first GetRow() works and why you crash on the second.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • and , shhh.. but casting to the correct type , and then calling the method on the casted type will prevent the crash – johnathan Dec 05 '12 at 08:12
  • You mean `((table)pBase)[1].GetRow()`? That will work, but it's a *horrible* thing to do and will almost certainly come back and bite you in the ass. – Nik Bougalis Dec 05 '12 at 08:21
  • yes i do mean exactly that. Yes it's quite 'horrible' , but it's quite a valid way to coax the compiler into accessing the polymorphic objects at the correct addresses. – johnathan Dec 05 '12 at 08:30
  • 1
    thanks guys, now I understand more about poly – Kaleidoscope Dec 05 '12 at 08:37
  • @NikBougalis: `((table)pBase)[1].GetRow()` is not guaranteed to work *(even after fixing the typo)*. You mean `dynamic_cast(pBase)[1].GetRow()`? That will work.
    – Nawaz Dec 05 '12 at 09:12
  • (oops - thanks for catching the typo). The `dynamic_cast<>` does something *very* different than the C style cast I showed, whose closest C++ equivalent would be `static_cast(pBase)[1].GetRow()`. The (typo-corrected) C-style cast is *guaranteed* to work correctly in this scenario, but as I pointed out it's horrible and will almost certainly come back and bite one in the ass.
    – Nik Bougalis Dec 05 '12 at 09:19
2

You need to create (with new) all those array elements, do it like this:

for(int i = 0; i < 3; ++i)
    pBase[i] = new table();
dreamzor
  • 5,795
  • 4
  • 41
  • 61