The common approach here is to throw an exception (and handle it somewhere higher up).
One of the benefits of the exception mechanism is that it allows you to throw an exception from within a class constructor. In that case, you never reach the situation where a pointer is returned to the invalid. You would "get control" at the corresponding catch block. If the pointer was only declared within the try block (or in some other method invoked by the try block), it would be outside your scope in that catch block.
That is not a hack - it is quite legitimate and a common programming technique.
For example, if your class constructor allocates memory dynamically (e.g., for an internal buffer) and this allocation failed, you would want to throw an exception because at the end of the constructor body you would not have a valid object.
Here is an example (I've been doing Java for the past 10 years, so my C++ code below is likely messed up here, maybe someone can edit this for me)
// Begin C++ code written by a Java programmer... :)
class Myclass
{
public:
Myclass(int length)
{
if(length<=0) throw BadBufferSizeException("Bla bla bla");
this->buffer = (char*)malloc(length*sizeof(char)); // don't remember the new syntax
}
void doSomething()
{
// Code for placing stuff in the buffer
}
private:
char* buffer;
};
int main()
{
try
{
int len;
len = getLengthFromUser();
MyClass* pMyClass = new MyClass(len);
myClass->doSomething();
} catch(const Exception & e)
{
// Whatever... Note how pMyClass is not even accessible here
}
}
Note that if you defined pMyclass to be null outside the try block, and then only reassigned it within the try block when you create the class, in the case of failure you would likely still have null, but you would never have executed doSomething(). If you are concerned about initialization, you could also move the doSomething() call to outside the try-catch block, but you would want to make sure that your pointer is not null.
Also note that C++ gives you more (too much?) freedom when it comes to throwing things than other languages. I usually like having a hierarchy of exception classes or using an existing library with such a hierarchy.