Passing a class to a sprintf() and similar
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Passing a class to a sprintf() and similar

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Question Passing a class to a sprintf() and similar

    I am trying to pass a class I wrote to sprintf(). Not the class itself actually, but a pointer to a string the class holds.

    With the VC++ CString class it works fine:
    sprintf(t, "%s", cst);

    But with my class, it either crashes the program or puts garbage into the t buffer:
    sprintf(t, "%s", c);

    Unless I use casting, and then it works ok:
    sprintf(t, "%s", (char *)c);

    What is my class missing? Some kind of an operator?
    Here is my code:


    class C1
    {
    public:
    C1()
    {
    sprintf(m_Data,"C1 class");
    }

    operator char *() { return m_Data; }

    private:
    char m_Data[100];
    };

    int main(int argc, char* argv[])
    {
    char t[1000]="";
    C1 c;
    CString cst("Test c string");

    sprintf(t, "%s", cst);
    sprintf(t, "%s", c);
    return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Passing a class to a sprintf() and similar

    The argument to sprintf must match the corresponding format specification. Therefore, you should explicitly cast to char* even though you have provided an implicit conversion function to char*.

    That said, an implicit conversion to char* is probably a bad idea as you might end up with unexpected conversions. I suggest that you either declare it explicit or provide a named member function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2008
    Posts
    1,131

    Re: Passing a class to a sprintf() and similar

    Quote Originally Posted by yaronkl67 View Post
    What is my class missing?
    just a lot of luck

    seriously, passing to printf ( or any C function vararg ) anything other than a POD with a matching format specifier will give undefined behavior ( ie, garbage data, a crashing program, an apparently working program, whatever etc ... ).

    Microsoft's CString is no exception and if you pass CString to printf you're writing non portable code that will (reliably) work only on Microsoft's compiler, where it works just because Microsoft said so.

    So, your class should either be a POD whose binary content can be safely interpreted as a null-terminated sequence of chars or, preferably, just expose a char* or CString accessor. Note that due to how C variadic arguments work, no conversion operator will take effect in this case. Even more preferably, you should avoid printf in C++ code altogether, in favor of one of the many type safe c++ string libraries ( iostream, boost.iostream, boot.format, ... ).

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center