0

Here are two classes

  class A{
    A(int val):Val(val){}
    int getVal(){return Val;}
    friend class B;
    private:
    int Val;
  }


 class B{
    B(A* ptr):PTR(ptr){}
    private:
    A* PTR;
  }

In the main function I create the objects for both classes

 A objA;
 B objB(&objA)

As you can see, objB now contains a pointer to the address of objA. The problem I am facing is how do I use a pointer from objB to access to member function in objA.

dramzy
  • 1,379
  • 1
  • 11
  • 25

1 Answers1

0

If you are looking to making objects of type A point at an object of type B and vice versa, you could use a setter instead of passing the instance through constructor arguments as such:

  class A{
        A(int val):Val(val){}
        int getVal(){return Val;)
        void setB(B* ptr) {BPtr= ptr;}
        friend class B;
        private:
        int Val;
        B* BPtr
  }


 class B{
        B(A* ptr):PTR(ptr){}
        private:
        A* PTR;
  }

And now, when you create A and B objects, you can do this:

 A objA;
 B objB(&objA);
 A.setB(&objB);

EDIT:

Ok, I misunderstood your question at first. If you're just looking to access a member of an object of type A, you would just do objA->field or (*objA).field .

dramzy
  • 1,379
  • 1
  • 11
  • 25