Accessing Child class Object from Another Child Class without creating new object in cpp

Viewed 36

I have a scenario where I need to access an object of a child class from another child class I have something like this: In parent.h

#include "child1.h"
#include "child2.h"

class Parent
{
public:

    Child1 Child1_obj;
    Child2 Child2_obj;

    void setVar2_Value(int v2)
    {
        Child2_obj.Var2 = v2;
    }
}; 

In child1.h

#include "child2.h"
class Child1
{
private:
    int Var1;
public:

    // these below functions are in .cpp file but I am placing here for simplicity to post
    int DoArithmetic()
    {
       int temp1 = Increment_and_Multiply();
       return temp1;
    }
    int Increment_and_Multiply()
    {
        Child2* pChild2;               // How to initialize so I can access the value of the same object created inside Class A, insted of pointing to new Child2 object
        
        // first increment Var2 and then multiply. I want this increment to be reflected  in object created in class A
        pChild2->Var2++;
        return pChild2->Var2 * Var1;
    }
};

In child2.h

#include "Child1.h"
class Child2
{
public:
    int Var2; 
};

In main.cpp

#include "parent.h"
int main()
{
    Parent P1;
    P1.setVar2_Value(5);            // set Var2 value to 5
    int res = P1.Child1_obj.DoArithmetic();
                                    // Var2 value in now 6
    // Do something else                                

}

As mentioned in the comments, I want Child2 class object access from Child1 so I can directly modify its values from Child1 which also get reflected in the object created in the parent class. Any guidance on this will be highly appreciated. Thank you.

1 Answers

Pass a reference to the child when you need access to it. Rather than calling Child1::DoArithmetic let the caller call Parent::Arithmetic:

class Child2
{
public:
    int Var2; 
};

class Child1
{
private:
    int Var1;
public:


    // these below functions are in .cpp file but I am placing here for simplicity to post
    int DoArithmetic(Child2& child2)
    {
       int temp1 = Increment_and_Multiply(child2);
       return temp1;
    }
    int Increment_and_Multiply(Child2& child2)
    {
        // first increment Var2 and then multiply. I want this increment to be reflected  in object created in class A
        child2.Var2++;
        return child2.Var2 * Var1;
    }
};


class Parent
{
public:
    Child1 Child1_obj;
    Child2 Child2_obj;
    int DoArithmetic() {
        return Child1_obj.DoArithmetic(Child2_obj);
    }
    void setVar2_Value(int v2)
    {
        Child2_obj.Var2 = v2;
    }
}; 

int main()
{
    Parent P1;
    P1.setVar2_Value(5);            // set Var2 value to 5
    int res = P1.DoArithmetic();
                                    // Var2 value in now 6
    // Do something else                                

}

PS: Do not define default constructors that do nothing. If you want to declare them then do Child1() = default;. There is also no need to declare the constructor when the compiler can geenrate it for you.

Related