I want to implement a class "class_B" that contains a pointer to an instance of another class "class_A" so that it can also access its member function. However, I do not know how to implement this. Below you can find my minimum working example. Could you please tell me what I am doing wrong?
#include <iostream>
class Class_A
{
public:
int number_;
// Here we define the constructor for "Class_A".
Class_A(int a)
{
number_ = a;
}
void printNumber()
{
std::cout << "The value in Class_A is " << number_ << std::endl;
}
};
class Class_B
{
public:
Class_A* class_A_instance;
// Here we define the constructor for "Class_B".
Class_B(int b)
{
class_A_instance = Class_A(b);
}
};
int main()
{
Class_B class_B_instance(3);
class_B_instance::class_A_instance::printNumber();
return 0;
}
I think you want to initialize your Class_A instance on the heap with new. Then call the non-static member class a instance of Class_B instance: