none
Use of const CString

    Question

  • Hi,Today I have reviewed some code of my team mate.

    One of the class contains a definition like this

    const CString BITMAP_FILE_NAME  = _T( "res/temp.bmp" );

    I suggested use LPCTSTR or const TCHAR* (PTCHAR) instead of CString because it has some more overheads that a normal string literal.

    So my question is.

    1. When a CString defined as const, does it really have some performance over heads?

    2. At the time of compilation, normally the the value of the const will be place in the code at compile time. For CString and other const object, how it is handling? is it replacing the address of the const object? As off my knowledge later will be happened.

    Could you please put some light on this?

    Friday, November 03, 2006 1:48 PM

Answers

  • Do you say that a class contains this

    const CString BITMAP_FILE_NAME  = _T( "res/temp.bmp" );

    ?!

    I ask because this does not compile. You can only initialize integral members in a class declaration.

    But to answer your questions:

    1. Well, it does. It's an object after all so it needs space for its members. Also CString will most probably allocate some memory using new to store the string assigned to it. Making it const does not change that.

    2. Placing the value of the const in the code happens for static integral type members. In case of objects there is not much difference from the way non-const objects are handled. A const member has storage allocated inside the class like any other member. The only difference is that the compiler won't let you change it (by calling non-const members of it for example).

     

    Friday, November 03, 2006 2:47 PM

All replies

  • Do you say that a class contains this

    const CString BITMAP_FILE_NAME  = _T( "res/temp.bmp" );

    ?!

    I ask because this does not compile. You can only initialize integral members in a class declaration.

    But to answer your questions:

    1. Well, it does. It's an object after all so it needs space for its members. Also CString will most probably allocate some memory using new to store the string assigned to it. Making it const does not change that.

    2. Placing the value of the const in the code happens for static integral type members. In case of objects there is not much difference from the way non-const objects are handled. A const member has storage allocated inside the class like any other member. The only difference is that the compiler won't let you change it (by calling non-const members of it for example).

     

    Friday, November 03, 2006 2:47 PM
  • Mike,
    Thanks alot for your reply.

    const CString BITMAP_FILE_NAME  = _T( "res/temp.bmp" );

    The above code compiling fine in my Visual C++ 2005.

    Could you please give suggestion? that to use CString, or constant string literals?
    Monday, November 06, 2006 5:50 AM
  • If it compiles then this must be a global variable. Anyway I'd go with a constant string literal (LPCTSTR). Using a const CString has exactly the same effect like using a non-const CString in terms of memory/speed. You'll get a static variable that has a constructor that needs to be called before the main/WinMain function can run. The const has no effect other than the fact that you cannot modify the string.

     

    Tuesday, November 07, 2006 2:41 PM
  •  Sarath. wrote:

    I suggested use LPCTSTR or const TCHAR* (PTCHAR) instead of CString because it has some more overheads that a normal string literal.

    What overheads? The creation and destruction of an object?

    So my question is.

    1. When a CString defined as const, does it really have some performance over heads?

    The const specifier only makes it read-only. In terms of constructing and destructing the object, nothing changes.

    2. At the time of compilation, normally the the value of the const will be place in the code at compile time. For CString and other const object, how it is handling? is it replacing the address of the const object? As off my knowledge later will be happened.

    Well, you can generate the assembly output files and see that for yourself. It depends on the kind of optimization. Basically it's treated like a constant object, i.e. an object that you cannot change its state.

    My question is: why do you really need this kind of optimization, replacing CString with LTCTSTR? What kind of project is it?

    Do you know the two rules of optimization?

    • don't optimize
    • don't optimize yet (for experts only)

     

    Tuesday, November 07, 2006 8:13 PM