d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso C++ Help
Prev12
Add Reply New Topic New Poll
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jul 4 2012 09:56am
Quote (MsRailgun @ Jul 3 2012 06:52pm)
Iso expertise.


i lol'd
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jul 4 2012 12:45pm
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.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jul 4 2012 09:43pm
Quote (MsRailgun @ Jul 4 2012 11:45am)

But I want another solution which will not use this notation.

why?
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jul 5 2012 05:21am
Ugh, forget my last question, there were some misinterpretations.
Otherwise, KrzaQ2 solved my original problem - once again, thanks to all who contributed.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll