I want to create a class that has an object (not a pointer) of another class as a private member. The other class accepts creating class's this pointer as constructor argument. However, I cant pass this from creating class's .h file as it shows a compile error. Of course, I can declare a pointer in the .h and new it in creating class's .cpp file and pass this as a constructor argument, but in this case I will be creating a pointer and not an object. I want to know how to create such an object?
class MyClass
{
private:
Parent* m_parent {nullptr};
public:
MyClass (Parent* parent) //The non-default constructor I want to invoke
{
m_parent = parent;
}
};
MyCode.h
class Parent
{
/*This class wants to have a private member object of type MyClass
and it wants to create this object by invoking its non-default parameter
and pass "this" as a parameter.
*/
private:
MyClass myclass;
//OPTION 1: This will invoke default constructor of MyClass, which I dont //want
Parent();
};
MyCode.cpp
Parent::Parent()
{
/*
Here I can create MyClass object and use its non-default constructor
However, the scope of this object is local and inaccessable outside of function
*/
MyClass myclass(this); //OPTION 2: this will invoke non-default constructor of myClass
}