Hey,
I have 2 classes:
class Base
{
public:
virtual int Foo(int n);
virtual void Goo() = 0;
virtual ~Base() ;
};
class Derived : public Base
{
public:
int Add4Bytes;
void Goo();
int Foo(int n);
};
int Test(Base* b)
{
for (int i=0;i<5;++i)
{
b->Foo(i);
++b;
}
return 0;
}
void Test2(Base arr[])
{
for (int i=0;i<5;++i)
{
arr[i].Foo(i);
}
}
void main
{
Base* b = new Derived[5];
Test(b);
}
So, when i'm calling Test, after the second loop there a memory viloation exception.
I have 2 questions:
- What's the difference between the function argument in Test and Test2 ? (Test2 doesn't compile after i turned Base into pure abstract class).
and the more important question
- How can i prevent that exception, and how can i pass an array of derived class to a function that suppose to get a base class array. (i can't tell in compile time which derived class i'm gonna pass the function)
p.s - please don't tell me to read Meyers book, that's the exact reason why i'm asking this question. :)
Thanks