-
Const function + new?
Having a small problem with a constant function that creates a new object via new and throws it. The compiler barks that it cannot create the new object due to (presumably) call to a constructor.
Does this work? And if it does, how? I can't seem to find how to make it work...
Code:
MemoryManager::CMemoryManagerException<T>* pException = new MemoryManager::CMemoryManagerException<T>(_T("CMemoryManager::Lock"),
_T("CMemoryManager::Lock"),
GetMemoryExceptionErrMsg(p),
MemoryManager::CMemoryManagerException<T>::ERR_COULD_NOT_ALLOCATE);
That's the line it fails to compile.
'CMemoryManagerBase<T>::GetMemoryExceptionErrMsg' : cannot convert 'this' pointer from 'const CMemoryManagerBase<T>' to 'CMemoryManagerBase<T> &'
It's obviously because the function is const.
-
Can you make the function const? If it's not modifying the class's state, then it should be const.
I'm assuming GetMemoryExceptionErrMsg() returns some kind of string? If you can't make it const for some reason, copy it to a string first, then pass that string.
-
You're right. I didn't notice it was for GetMemoryExceptionErrMsg().
It should be const. Thanks.
Sometimes we make silly mistakes. Hehe.