Suppose I have a code:
Class A have items which is pushed into a vector...I want to access Class A element from class B through A's vector..

class A
{
    friend class B;
    vector<A> data; //store info in vector
};

Class B
{
    void showDataInClassA();
    A a;

}
void B::showDataInClassA()
{
    vector<A>::iterator p= a.data.begin();
    while(p != a.data.end())
    {
        //cout<<p->getUserName();
        ++p;
      }

}

However, it doesnt show up. May I get any advice?
Thanks.

Dani AI

Generated

The loop printed nothing because the A instance you iterate (B::a) is not the same A instance that was populated. Making B a friend of A only grants access to members — it does not share data between separate instances. created an A inside B, so its vector was empty. demonstrated a working alternative by using shared storage, but that example used raw new and raw pointers, which requires careful memory management.

Three practical fixes, ordered by safety and clarity:

  • Keep per-instance storage and give B a reference (or pointer) to the populated A (preferred). Add an accessor on A and iterate the real instance, not a fresh one inside B.
  • Make the container a shared resource (static or a separate manager/singleton) with controlled accessors if the data truly must be global.
  • If you need dynamic polymorphism, store smart pointers (std::unique_ptr or std::shared_ptr) instead of raw pointers to avoid leaks.

Example of the preferred approach (use a getter and pass the populated A in):

// in A
const std::vector<A>& getData() const { return data; }

// in B
void B::showDataInClassA(const A& source)
{
  for (const A& item : source.getData())
    std::cout << item.getUserName() << '\n';
}

Notes and cautions: prefer storing objects by value when possible, or unique_ptr for ownership. If you make the container static, keep it private and expose controlled accessors. Avoid relying on friend to solve ownership/visibility problems — it only affects access control, not which instance you are using. Quick debug tip: print the address of the A instance you fill and the one B uses to confirm they are the same or different.

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.