Code
#include <iostream>
#include <vector>
using namespace std;
int ii = 90;
class Base{
protected:
int i;
public:
Base(int i) { cout << "INITIALIZED" << endl; }
virtual void setnum(int u) = 0;
virtual void doSomething(){
cout << " - - - - " << endl;
}
};
// - - - - - - - - - - - - - - - - - - - - - - - -
class Extended : public Base{
public:
Extended(int z) {
i = z;
}
void setnum(int u) {
i = u;
}
void doSomething(){
cout << "Doing something..." << i << endl;
}
};
typedef vector<Base*> BVector;
void helper_func(BVector& objs) {
BVector temp;
temp = objs;
temp[1]->setnum(1338);
}
int main(int arg, char* argv[]) {
Extended *extensionObj = new Extended(1337);
vector<Base*> head(2);
head[1] = NULL;
head[2]= NULL;
vector<Base*> objs;
head[0] = new Extended(999);
head[1] = extensionObj;
objs = head;
objs[0]->doSomething();
objs[1]->doSomething();
helper_func(objs);
objs[1]->doSomething();
cout << ii << endl;
ii = ii + 1;
cout << ii << endl;
delete objs[0];
delete objs[1];
return 0;
}
There's a compile error involving the constructors - I hope someone can shed some insight and a solution?
I understand there's 1 way to get it working by
public:
Extended(int z) : Base(z) {
i = z;
}
But I want another solution which will not use this notation.