return; in constructor???
-
Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j
-
Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j
While it would be syntactically correct, it doesn't make much sense.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j
patnsnaudy wrote: I would assume that it skips the execution of the code below and returns a pointer to the object. What do you mean by that? Constructors return nothing.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
While it would be syntactically correct, it doesn't make much sense.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
Thanks for the reply, but I am still not sure. I am reviewing some code and this person uses return in a lot of constructors. But, I am with you.
-
patnsnaudy wrote: I would assume that it skips the execution of the code below and returns a pointer to the object. What do you mean by that? Constructors return nothing.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... }
I assume that "do some more stuff" would not get executed. -
Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j
-
ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... }
I assume that "do some more stuff" would not get executed.patnsnaudy wrote: I assume that "do some more stuff" would not get executed. Correct! Since
if (true)
is always true. it will return without executing "do some more stuff" Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
That's the answer I was looking for. You win the prize :beer:
-
ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... }
I assume that "do some more stuff" would not get executed.That's correct. But you also wrote and returns a pointer to the object, which is not true.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
That's correct. But you also wrote and returns a pointer to the object, which is not true.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
I guess that I meant when you new the object that a pointer to the object would be passed back. The one thing that I thought return in a ctor may do is pass back null and not create the object so: ctor a = new ctor(); // thought that it was possible that a would be null if ctor called return, but now I know that is not the case. I could have tested it, but didn't want to spend the time. :-O