ZoOl007 ZoOl007 - 7 months ago 43
C++ Question

c++ pass pointer between classes - access other class methods

I have this class instance

ControlPanel *cp = new ControlPanel();

Another class needs access to the methods in cp so I

Monitor *cpmon = new Monitor(cp);

The Monitor class header looks like

class Monitor {
Monitor(ControlPanel *_cp);
ControlPanel *cp;

and the Monitor constructor looks like

#include "Monitor.h"

Monitor::Monitor(ControlPanel *_cp)
*cp = _cp; //doesn't work

error is :

no match for ‘operator=’ (operand types are ‘ControlPanel’ and ‘ControlPanel*’)

now I printed the pointer addresses along the way and I can access the methods from within the constructor eg if I do _cp->SomeMethod it works but I don't know how I can code that the private *cp actually is the same address _cp points to so that I can access the cp intantiated at the very top from within Monitor's methods and not just from within its constructor - I read a lot - tried a zillion things, got a lot of different errors but I just can't make it work. Can someone clarify? How do I assign private *cp _cp's address please?
thank you


The problem is you dereference the pointer and assign it to a pointer. To fix, simply change from

*cp = _cp;


cp = _cp;