CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    whats the deal with _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Do we really need the macro that vc 7 put in to its stdafx.h file.
    _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    I would like to know if not having this is a performance hit or not and what are the pros and cons for this macro.

    Thank you all.

    IK
    and Canada rocks!! Peace bro.

  2. #2
    Join Date
    Sep 2004
    Posts
    519

    Re: whats the deal with _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Quote Originally Posted by MSDN
    ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Makes certain CString constructors explicit, preventing any unintentional conversions
    In C++ it means that the constructors are declared something like below:
    Code:
    struct my_string
    {
        explicit my_string(char const * const);
    };
    
    
    void f(my_string const & s);
    
    int main()
    {
        f(my_string("Test")); // 1. works
        f("Test"); // 2. doesn't work because of explicit
    }
    If you leave out explicit it means that for 2. above the compiler could implicitly construct a my_string object from the c-string and the code above would compile.

    Performance wise it's no difference between 1. and 2. However, in practice by allowing implicit constructors a programmer not aware of the implicit constructors might create unnecessary objects.

    Personally I prefer explicit constructors as in my opinion it leads to more robust code (you have to specify what you want) but I admit I'm occassionally tempted by the lures of convenience promised by the implicit constructors.

    Hope this helps,

    MÃ¥rten

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: whats the deal with _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Quote Originally Posted by IndikaNiva
    I would like to know if not having this is a performance hit or not and what are the pros and cons for this macro.
    Looking up MSDN always helps.

    This macro results in CString being explicitly constructed.
    Quote Originally Posted by IndikaNiva
    Do we really need the macro that vc 7 put in to its stdafx.h file.
    VC++ defines many macros that affect your build.

    If I were you, I wouldn't tamper them...
    Last edited by Siddhartha; August 31st, 2005 at 10:35 AM.

  4. #4
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Re: whats the deal with _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Well I have given this booooring task of converting like 60 COM DLL projects to unicode.
    So I figure if I put this macro in it will generate compiler errors for strings taht dont have _T or L in unicode.

    But I find out that in regualr debug this macro will generate an error for
    BSTR bsTest = SysAllocString(_T("blah");

    CString szString = bsTest // Error.

    So I'm just exploring my options here.

    Thanks for the input
    and Canada rocks!! Peace bro.

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: whats the deal with _ATL_CSTRING_EXPLICIT_CONSTRUCTORS

    Quote Originally Posted by IndikaNiva
    But I find out that in regualr debug this macro will generate an error for
    BSTR bsTest = SysAllocString(_T("blah");

    CString szString = bsTest // Error.
    I told you already in this thread that CString can wrap LPTSTR from both Unicode and Non-Unicode source strings using overloaded constructors in CStringT - the template class it specializes.

    With _ATL_CSTRING_EXPLICIT_CONSTRUCTORS defined, the correct way would be -
    Code:
    CString szString ("blah");
    Note that you dont need to do the SysAllocString stuff.

    When there are simple ways to do something, I am not able to understand why you try the far-fetched?

    Besides, if you are insistent on using this -
    Code:
    CString szString = bsTest;
    ...Comment out the macro defined in StdAfx.h

    However, I prefer the former method that does not require exclusion of this macro.
    Last edited by Siddhartha; August 31st, 2005 at 11:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured