0

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 :)

3
  • 4
    if(!myThing) doesn't actually make any sense. myThing is a type. Commented Jan 15, 2014 at 16:46
  • 1
    It's not quite clear what you're asking. if (!myThing) is nonsense; myThing is a type name not a pointer. You can check the pointers within myModule &m with if (!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. Commented Jan 15, 2014 at 16:56
  • You're right. That was a typo. @MikeSeymour, I see your point. Yes, I want to know if they are null or not. Commented Jan 15, 2014 at 20:34

3 Answers 3

1

You need to initialize the in, out in the constructor.

Btw, if(!myThing) will not even compile as myThing is declared or defined anywhere.

Sign up to request clarification or add additional context in comments.

Comments

1

to check if a pointer is null - just check that the pointer is null if(ptr==NULL) ...

keep in mind that a pointer isn't null at declaration since it will not do a self initialization. so if u really like to check for a NULL pointer u should set it to NULL.

btw: i would suggest u go one with c++ references, that's much safer at all. regards

1 Comment

If you can be sure of C++11 support, if ( ptr == nullptr ) would be better. (If not your if ( ptr == NULL ) is to be preferred.)
0

As many answered, it is necessary to initializemyThing objects to NULL in the constructor of myObject instead of in the parent. Then, both if(!in) and if(in == NULL) work.

Thanks!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.