Hello all. It's been a while since I last posted here but I'm back in the swing of things and I have two doozies for any of you if you have the time.
First off I have a question about the vector STL. I know that single dimensional vectors have the iterators that can navigate both forward and backward though the vector and you can also access the vector elements with subscripts. But I was wondering about multidimensional vectors. My question about these is two fold. Can you use an iterator to navigate through a multidimensional vector or do you have to use the two for loops to flip though the vector in a row-by-row fashion? And if you can use an iterator, can you get away with using only one or do you have to use two iterators, one for each dimension?
Second is a question about nested classes. I was playing around with these in a very small program that I figured would allow me to test out the concepts and see for myself. I wrote the program and it compiled fine however the output was in no way what I was expecting. Here is the program:
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
class A {
protected:
int d;
public:
class SubA {
protected:
int a;
public:
SubA() {};
~SubA() {};
int getA() { return a; }
void setA(int _a) { _a = a; }
};
class SubB {
protected:
int b;
public:
SubB() {};
~SubB() {};
int getB() { return b; }
void setB(int _b) { _b = b; }
};
class SubC {
protected:
int c;
public:
SubC() {};
~SubC() {};
int getC() { return c; }
void setC(int _c) { _c = c; }
};
A() {};
~A() {};
int getD() { return d; }
void setD(int _d) { _d = d; }
};
int main(int argc, char *argv[]) {
//instantiate all the classes
A dummy;
A::SubA subADummy;
A::SubB subBDummy;
A::SubC subCDummy;
//set up some values
dummy.setD(5);
subADummy.setA(10);
subBDummy.setB(15);
subCDummy.setC(20);
//print them out to the screen
cout << dummy.getD() << endl;
cout << subADummy.getA() << endl;
cout << subBDummy.getB() << endl;
cout << subCDummy.getC() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Now as I'm sure you can see from the code the output that I was expecting. However what I got was totally different. It looked more like this:
2013315389
2013327107
2013454920
7863840
Because the numbers are related somehow (the first three anyway) I'm guessing that this is a location in memory rather than the actual values I passed to the classes. Or I could be wrong. I'm not too sure. But if I could get some help with these topics I'd appreciate it. Thanks in advance. It's much appreciated.