I am new to c++, I wanted to know how to return null if a condition. Also, wanted to know what is the best practice in these occasion because I could just create this function by knowing not to call if the condition is not met, but usually with C#, Java, Javascript, and other OOP we do checks before returning due to invalid values. Please keep in mind I work with a really old cpp compiler. Example:
MyObject MyObject::createObject(int id){
if(id < 0) return nullptr;
return new MyObject(id);
}
Edit: I had a really on C++ compiler which does not accept C++11 or above. Please don't be rude on the criticism. It's just that I am stuck with old compilers and can't do anything about that one. I do not want to return a pointer because that will be more mess to go with. I just would like to know what could be done and/or what will be the best practice for this issues. Thank you in advance!
Edit 2: After finding out nullptr was something that was not declared in old version of c++, I had to use NULL to be able to return nullptr since the error kept saying it was not declared.
My answer looked like:
MyObject MyObject::createObject(int id){
if(id < 0) return NULL;
return new MyObject(id);
}