In the following code, I want main() to have access to some data that it doesn't (and shouldn't) have access to:
Code:
#include <vector>
using namespace std;
template <typename T>
class A
{
private:
class B
{
public:
B() { count = 0 }
T data;
private:
unsigned int count;
friend class A;
};
public:
A();
B* do_stuff(T some_data);
};
template <typename T>
typename A<T>::B* A<T>::do_stuff(T some_data)
{
B* temp = new B;
temp->data = some_data;
++(temp->count);
return temp;
}
int main()
{
A<int> MyClass;
vector<A<int>::B*> my_vector;
for(int i = 0; i < 10; ++i)
{
my_vector.push_back(MyClass.do_stuff(i));
}
return 0;
}
So, in main() there's a call to A::do_stuff(), which returns a pointer to B. The problem is that I want to store these pointers in a vector, but I can't make a vector of B* in main(), because it's not public. I don't want the whole world to have access to B, and I don't need to modify B directly (other than by the "correct" way) - I just need to store some pointers. What's the right way to go about doing this without breaking things?