MemoryManager openMemory() {
if (...) {
return memory_manager_instance;
}
else
return NULL;
}
MemoryManager is the name of a user defined C++ class. The function definition above gives me the error in the title.
Basically I don’t want to return an instance when the condition does not hold. Such definition of function is valid and what I usually do in Java, but it seems not working in C++. What should I do to tackle this?
Have your function return a
MemoryManager *(perhaps using one of the several smart pointer classes available in the C++ library or in boost). IfMemoryManageris the name of a class, then this function as written returns a copy ofmemory_manager_instance. This is different from Java, where a variable of typeMemoryManagerwould be a reference to an object.EDIT: Further, it looks like you are trying to implement a singleton. You’d want to make the default constructor, copy constructor, and assignment operator for
MemoryManagerprivate or protected. The first two are in Java as well, but not the last!