dongju.jeong Posted September 18, 2020 Share Posted September 18, 2020 (edited) Class A : ComponentBase { public: ... PROP_PARAM(Node, Sample1); public: NodePtr Sample2; B tempB; } //////////////////////////////// Class A::init() { Sample2 = node->getChild(1); tempB.tempA = this; } //----------------------------------------- Class B { ... A* tempA; void func(); } ///////////////////////////// void B::func() { const char* name1 = tempA->Sample1.get()->getName(); // error const char* name2 = tempA->Sample2->getName(); // pointer error (pointer is Destroied) } How do I use pointers correctly in the C++ component system? Edited September 21, 2020 by dongju.jeong Link to comment
cash-metall Posted September 18, 2020 Share Posted September 18, 2020 check if this node exists before getting the name if (tempA->Sample1) const char* name1 = tempA->Sample1.get()->getName(); if (tempA->Sample2) const char* name2 = tempA->Sample2->getName(); I also recommend checking nodes during initialization Class A::init() { Sample2 = node->getChild(1); if (!Sample2) Log::error("Node sample2 doesnt exist!\n"); tempB.tempA = this; } Link to comment
dongju.jeong Posted September 21, 2020 Author Share Posted September 21, 2020 (edited) Nodes exist... Sample 1 and sample2 can be used normally inside Class A. However, if I try to use Sample1 and Sample2 using Class A pointer variables within Class B, an error occurs. There was no error when using this method. Class A : ComponentBase { public: ... PROP_PARAM(Node, Sample1); public: NodePtr Sample2; B tempB; NodePtr getSample1() { return Sample1.get(); } NodePtr getSample2() { return Sample2; } } //////////////////////////////// Class A::init() { Sample2 = node->getChild(1); tempB.tempA = this; } //----------------------------------------- Class B { ... A* tempA; void func(); } ///////////////////////////// void B::func() { const char* name1 = tempA->getSample1()->getName(); // don't error const char* name2 = tempA->getSample2()->getName(); // } Edited September 21, 2020 by dongju.jeong Link to comment
Recommended Posts