the vector is not populated with anything.
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class B;
class A
{
public:
friend B;
A(string n) {username = n;}
A() {}
static vector<A*> data; //store info in vector
string getUserName() {return username;}
protected:
string username;
};
class B
{
public:
B() {}
void showDataInClassA();
A a;
};
void B::showDataInClassA()
{
vector<A*>::iterator p= a.data.begin();
while(p != a.data.end())
{
cout<< (*p)->getUserName();
++p;
}
}
vector<A*> A::data; //store info in vector
int main()
{
A::data.push_back(new A("One"));
A::data.push_back(new A("Two"));
A::data.push_back(new A("Three"));
B b;
b.showDataInClassA();
return 0;
}
Ancient Dragon
Achieved Level 70
32,137 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69