class Base {
public:
virtual void f(){ cout << "in f() of Base..." << endl;};
};
class Derived : public Base {
public:
private:
int i_;
};
void userCode(Base* arrayOfBase) {
arrayOfBase[1].f();
}
int main()
{
Derived arrayOfDerived[10];
userCode(arrayOfDerived);
}
The problem is that the array created is of 80 bytes
(each object of Derived is 8 and Base is 4).
Hence arrayOfBase[1] would point only 4 bytes ahead of arrayOfBase when actually the 2nd object starts 8 bytes ahead.
One easy way to solve this is to rename the signature
void userCode(Base*) to
void userCode(Derived*)
but is there some other solution in which I can retain
void userCode(Base* ) ?
Any casts that will cast Derived[] to Base[]
public:
virtual void f(){ cout << "in f() of Base..." << endl;};
};
class Derived : public Base {
public:
private:
int i_;
};
void userCode(Base* arrayOfBase) {
arrayOfBase[1].f();
}
int main()
{
Derived arrayOfDerived[10];
userCode(arrayOfDerived);
}
The problem is that the array created is of 80 bytes
(each object of Derived is 8 and Base is 4).
Hence arrayOfBase[1] would point only 4 bytes ahead of arrayOfBase when actually the 2nd object starts 8 bytes ahead.
One easy way to solve this is to rename the signature
void userCode(Base*) to
void userCode(Derived*)
but is there some other solution in which I can retain
void userCode(Base* ) ?
Any casts that will cast Derived[] to Base[]