2

I have two classes A, B of which I want to create the object of class B from class A only. I do not want other classes to create an object of the class B. Here is the code snippet. Any suggestion how can I achieve without nested class? Can somebody please advice what is the correct approach to solving this problem?

class B
{
public:
    B(int x1, int y1, int x2, int y2);
    ~B();

    updateCoordinates(int x1, int y1, int x2, int y2);

private:
    int x1;
    int y1;
    int x2;
    int y2;
};

class A
{
public:
    A(int mode);
    ~A();

private:
    vector<B> bList;
};

A::A()
{
    // Based on the value of mod, create
    // objects of B and add to bList
}
Venkata Subbarao
  • 352
  • 4
  • 22

1 Answers1

2
class B
{
    friend class A;
    private:
        B(int x1, int y1, int x2, int y2);
    ...
}
  • 1
    After creating the object, I would like to give the handle to another object so that it can invoke one public function of B. I assume that is possible with this approach and not bad idea – Venkata Subbarao Mar 23 '18 at 14:12