Thread: Const function + new?

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're right. I didn't notice it was for GetMemoryExceptionErrMsg().
    It should be const. Thanks.
    Sometimes we make silly mistakes. Hehe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. help: function returns typedef const
    By waddavis in forum C Programming
    Replies: 4
    Last Post: 12-11-2001, 10:19 AM