2

I want to create two objects A and B and each object contains each other.

class B;

class A
{
public:
    A(B * b) : _b(b) {}
    void printB(void)
    {
        if (0 != _b)
        {
            std::cout << "B is not null" << std::endl;
        }
        else
        {
            std::cout << "B is null" << std::endl;
        }
    }
private:
    B * _b;
};

class B
{
public:
    B(A * a) : _a(a) {}
    void printA(void)
    {
        if (0 != _a)
        {
            std::cout << "A is not null" << std::endl;
        }
        else
        {
            std::cout << "A is null" << std::endl;
        }
    }
private:
    A * _a;
};

int main(int argc, char ** argv)
{
    A * a = 0;
    B * b = 0;

    a = new A(b);
    b = new B(a);

    a->printB();
    b->printA();

    delete a;
    delete b;

    return 0;
}

As you can see object 'a' contains null pointer 'b'. What is the best way to re-write this code so that 'a' contains a reference to 'b'? (note that object 'a' and 'b' needs to use 'new')

Many thanks!

4
  • 1
    Two objects cannot contain one another, since every object is the subobject of a unique complete object. Commented Jun 2, 2014 at 8:37
  • 1
    you should use setter after object creation. Otherwise one has to be null because of circular reference. Commented Jun 2, 2014 at 8:37
  • References cannot be null. So you want instance of A to hold a reference to instance of B, which holds a reference to instance of A, which holds a reference to instance of B, which holds... Commented Jun 2, 2014 at 8:48
  • @chesschi, I am assuming that you mean pointer when you say reference because you have not yet learned about C++ References yet. If that is what you mean, it would be best if you edited your question appropriately. Commented Jun 2, 2014 at 8:54

1 Answer 1

6

Just add a setB() method and call it after you are done constructing both.

#include <iostream>

class B;

class A
{
public:
    A(B * b) : _b(b) {}
    void setB(B* b) {
        this->_b = b;
    }
    void printB(void)
    {
        if (0 != _b)
        {
            std::cout << "B is not null" << std::endl;
        }
        else
        {
            std::cout << "B is null" << std::endl;
        }
    }
private:
    B * _b;
};

class B
{
public:
    B(A * a) : _a(a) {}
    void printA(void)
    {
        if (0 != _a)
        {
            std::cout << "A is not null" << std::endl;
        }
        else
        {
            std::cout << "A is null" << std::endl;
        }
    }
private:
    A * _a;
};

int main(int argc, char ** argv)
{
    A * a = 0;
    B * b = 0;

    a = new A(b);
    b = new B(a);
    a->setB(b);

    a->printB();
    b->printA();

    delete a;
    delete b;

    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

How does it solve the problem What is the best way to re-write this code so that 'a' contains a reference to 'b' ? A still contains a pointer to B instead of a reference. Or maybe I got the question wrong.
@Spook, Given the phrasing of the OP's question and the structure of his current code, I deduce that he means pointer when he says reference, rather than a literal & reference.
Yeah, reference in C++ means something very specific. If it is the case, this seems to be a proper solution.
Sorry about that if it is not clear. Basically what I meant is when B changes, A will get B's latest change and vice versa. And the setter is the solution. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.