L
Lou Pecora
Problem: If I inherit an object B from A and use references in B to
data in A, arrays of B objects which are pointed to by an A pointer
causes "EXC_BAD_ACCESS" errors at the point shown in the code below.
Removing the references cures the problem. Throughout, the correct
virtural functions are called.
The code is dirt simple (below). Can anyone explain why reference in
the interited object ruin the array structure in some way that causes a
crash? If I remove the references in B (w and z) and their
initialization in the B creator, the code runs fine and the correct
(virtual) B:rn_data() is called.
I'm running gcc 3.3 on a Mac OS X 10.3.9 Powerbook using Xcode. But
this seems like a generic C++ question.
Thanks for any help.
--- The code - header -----------------------------------
// junk.h
class A {
public:
float x,y;
A();
virtual void prn_data();
};
class B: public A {
public:
float &w,&z;
B();
virtual void prn_data();
};
--- The code - source .cpp -----------------------------------
// ---- Junk Test --------------------------
void junktest() {
A *pb=new B[2]; // NOTE: the pointer is of type A.
pb[0].prn_data();
pb[1].prn_data(); // <--- ERROR here
}
// --- A Creator -----------------
A::A() {
x=1.0; y=2.0;
}
// Print data -- virtual
void A:rn_data() {
printf("In A\n");
}
// --- B Creator -----------------
B::B():A(), w(x), z(y)
{
}
// Print data
void B:rn_data() {
printf("In B\n");
A:rn_data();
}
-- Lou Pecora (my views are my own) REMOVE THIS to email me.
data in A, arrays of B objects which are pointed to by an A pointer
causes "EXC_BAD_ACCESS" errors at the point shown in the code below.
Removing the references cures the problem. Throughout, the correct
virtural functions are called.
The code is dirt simple (below). Can anyone explain why reference in
the interited object ruin the array structure in some way that causes a
crash? If I remove the references in B (w and z) and their
initialization in the B creator, the code runs fine and the correct
(virtual) B:rn_data() is called.
I'm running gcc 3.3 on a Mac OS X 10.3.9 Powerbook using Xcode. But
this seems like a generic C++ question.
Thanks for any help.
--- The code - header -----------------------------------
// junk.h
class A {
public:
float x,y;
A();
virtual void prn_data();
};
class B: public A {
public:
float &w,&z;
B();
virtual void prn_data();
};
--- The code - source .cpp -----------------------------------
// ---- Junk Test --------------------------
void junktest() {
A *pb=new B[2]; // NOTE: the pointer is of type A.
pb[0].prn_data();
pb[1].prn_data(); // <--- ERROR here
}
// --- A Creator -----------------
A::A() {
x=1.0; y=2.0;
}
// Print data -- virtual
void A:rn_data() {
printf("In A\n");
}
// --- B Creator -----------------
B::B():A(), w(x), z(y)
{
}
// Print data
void B:rn_data() {
printf("In B\n");
A:rn_data();
}
-- Lou Pecora (my views are my own) REMOVE THIS to email me.