I would like to know how can I pass pointer to an array in a function and inside the function I still want to use it as a pointer.
process(Shape **sp);
int main(){
Shape* objShape[5];
process(objShape);
}
process(Shape **sp) {
sp[0]=&objRec; // No Errors
cout<<sp[0]->computeArea(); // WORKS
}
BUT WHEN I SHIFT THE COMPUTEAREA() METHOD TO ANOTHER FUNCTION IT DOES NOT WORK
getArea(Shape **sp){
cout<<sp[0]->computeArea(); // I get a segmentation fault error;
}
objRec is a child class of Shape class. And I need to use Dynamic binding to assess the compute Area.
Simple, like this
You had yourself confused with all the pointers. You have a pointer to an array of objects. But your code was written as if you had a pointer to an array of pointers to objects, which isn’t the same thing at all.
In your second piece of code you do have an array of pointers which is why your second piece of code compiles.
As chris points out you also need to lose the
const.