Here's the question;
The following classes and main are from
http://cboard.cprogramming.com/cplusplus-programming/100352-array-base-abstract-class-problem.html#include <iostream>
class Base{
public:
virtual void doSomething(){
std::cout << " - - - - " << std::endl;
}
};
// - - - - - - - - - - - - - - - - - - - - - - - -
class Extended : public Base{
public:
void doSomething(){
std::cout << "Doing something..." << std::endl;
}
};
// - - - - - - - - - - - - - - - - - - - - - - - -
// ------ NEW
void helper_func(Base* objs[]) {
objs[0]->doSomething();
}
//-----
int main(int argc, char* argv[]){
Base baseObj;
Extended *extensionObj = new Extended;
Base *objs[2];
objs[0] = &baseObj;
objs[1] = extensionObj;
objs[0]->doSomething();
objs[1]->doSomething();
///--- new
helper_func(objs);
//----
return 0;
}
Now the question is, if we take at the routine helper_func and the call to it in main, how would I be able to replace:
void helper_func(Base* objs[])
to
void helper_func(Base& ...) where .. is whatever, but Base& must be there - since I want to use a reference. (This is a specification).
In reflection of this change, what would be the change to the call?: helper_func(objs);
I still want it to do exactly the same thing as above, where the array of pointers to a base can be accessed "normally" in another function.
Question 2: I want to clarify if the following will delete the memory reserved.
delete objs[0];
delete objs[2];
Thank you~
100 fg to the first correct solution.
This post was edited by MsRailgun on Jul 4 2012 06:57am