I know, there are a lot of questions like this. How do I check if a pointer is NULL. I did research about this here and outside, finding some pretty good answers, but apparently they do not work for me.
I have this structure:
myModule.h
class myModule { public: myThing *in; myThing *out; virtual ~myModule() {} virtual myModule& bind(myModule &m) = 0; }; myThing.h
class myThing { public: myThing() {} node *get_node() { return &n; } signal *get_signal() { return &s; } private: node n; signal s; }; mySubModule.h
class mySubModule : public myModule { public: virtual ~mySubModule() {} virtual myModule& bind(myModule &m) { // Here comes the 'checkpoint' if(!in) in = new myThing(); in.doSomething(); } }; myObject.h
class myObject : public mySubModule { // This is the 'real' object that will call 'bind' }; So when it goes through the 'checkpoint' it's always pointing to something, so it never goes inside that 'if'. I guess it something about the inheritance, because the compiler does not know what myThing is or whatever, but it would be really nice to make that 'checkpoint' work. I tried initializing myThing *in, *out to NULL, but it won't work.
Plus, maybe there is something wrong with the interface myModule because of the virtual functions. I'm not sure that is correct. Could it be the reason?
If anyone comes up with an idea, it would be awesome.
EDIT
I just realized that there was a typo. The 'checkpoint' was wrong, it was if(!in) instead of if(!myThing).
My intention with this checkpoint is to check if the object (myThing *in) is NULL. If it is NULL, I would initialize it. If it is not, I would use it.
I have already tried this:
class myModule { ... myModule() { in = NULL; out = NULL; } ... } But it is not working for me. if(!in) or if(in == NULL) is returning false.
Should I initialize it in the myObject constructor? Can't I do it in the parent constructor?
Thanks a lot :)
if(!myThing)doesn't actually make any sense.myThingis a type.if (!myThing)is nonsense;myThingis a type name not a pointer. You can check the pointers withinmyModule &mwithif (!m.in || !m.out), if that's what you want. Note that this doesn't test whether they're valid, just whether they're null. Your class doesn't initialise them, so they'll probably be neither valid nor null unless something else has given them sensible values.