Base pointer Blues!!!
-
Suppose i have a set up like this:
class Base
{
public:
int x;
virtual void f()
};
class Derv:public Base
{
public:
int y;
void f();
};void main()
{
Base *p;
p=new Derv[4];//I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE for(int i=0;i<4;i++) (p+i)->y=10; //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW delete\[\]p;
}
NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
Suppose i have a set up like this:
class Base
{
public:
int x;
virtual void f()
};
class Derv:public Base
{
public:
int y;
void f();
};void main()
{
Base *p;
p=new Derv[4];//I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE for(int i=0;i<4;i++) (p+i)->y=10; //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW delete\[\]p;
}
NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
Hey Avenger, Here's a funny workaround... DWORD dw = (DWORD) p; // move in the array using this Base* p2 = NULL; for(int i=0;i<4;i++) { p2 = (Base*) dw; p2->y=10; dw += sizeof(Derv); // move to the next elem } What do you think?? :-D Clinton
-
Suppose i have a set up like this:
class Base
{
public:
int x;
virtual void f()
};
class Derv:public Base
{
public:
int y;
void f();
};void main()
{
Base *p;
p=new Derv[4];//I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE for(int i=0;i<4;i++) (p+i)->y=10; //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW delete\[\]p;
}
NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
hey, looking your code, i cannot see any inheritence between your two classes. you must tell that Derv is derived from Base (public, protected or private as you like...) :
class Base {
public:
int x;
virtual void f();
};class Derv : public Base {
public:
int y;
void f();
};avenger_sb25 wrote: Suppose the derived class array was allocated at 0x00000000 impossible, nothing can be allocated at the address 0x00000000 (and even the operating system don't !). you must test if the address returned by
new
is different from NULL ((void*)0x00000000
) :Base* p;
p = new Derv[4];
if (p != NULL) {
for (int i = 0; i < 4; i++) {
p[i]->y = 10;
}
delete[] p;
}
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Hey Avenger, Here's a funny workaround... DWORD dw = (DWORD) p; // move in the array using this Base* p2 = NULL; for(int i=0;i<4;i++) { p2 = (Base*) dw; p2->y=10; dw += sizeof(Derv); // move to the next elem } What do you think?? :-D Clinton
Nice trying Clinton!:-D If you come up with a better solution, do let me know chao!:rose: ...Avenger
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs