Click here to Skip to main content
15,896,154 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Passing a class to a sprintf() and similar Pin
Crazy Joe Devola14-Sep-14 20:07
Crazy Joe Devola14-Sep-14 20:07 
GeneralRe: Passing a class to a sprintf() and similar Pin
CPallini15-Sep-14 2:15
mveCPallini15-Sep-14 2:15 
GeneralRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 2:59
mveRichard MacCutchan16-Sep-14 2:59 
GeneralRe: Passing a class to a sprintf() and similar Pin
CPallini16-Sep-14 3:11
mveCPallini16-Sep-14 3:11 
GeneralRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 3:40
mveRichard MacCutchan16-Sep-14 3:40 
GeneralRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 4:04
mveRichard MacCutchan16-Sep-14 4:04 
GeneralRe: Passing a class to a sprintf() and similar Pin
Bram van Kampen16-Sep-14 16:45
Bram van Kampen16-Sep-14 16:45 
AnswerRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 4:04
mveRichard MacCutchan16-Sep-14 4:04 
OK, I finally figured it out. CString is a bit more of a sophisticated class, and will not use a fixed length buffer for its data. It uses a dynamically allocated one, and as long as that is the first data item in the class definition, the sprintf call will work; although it is luck rather than anything else. You can check this out with the following code:
C++
class C1
{
public:
    C1()
    {
        // allocate space for the text and then copy to the dynamic buffer
        m_Data = (char*)malloc(100);
        sprintf(m_Data,"C1 class");
    }
    
    operator char *() { return m_Data; }
    
private:
    char* m_Data; // make buffer a pointer
    
    };

When you now use the object without the cast, it just copies the object's data onto the stack, and since this data is a pointer to the actual text it all works as you expect. But be aware that this may have unintended consequences, so always use the cast when you want this behaviour.
GeneralRe: Passing a class to a sprintf() and similar Pin
CPallini16-Sep-14 4:33
mveCPallini16-Sep-14 4:33 
GeneralRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 5:03
mveRichard MacCutchan16-Sep-14 5:03 
GeneralRe: Passing a class to a sprintf() and similar Pin
CPallini16-Sep-14 5:13
mveCPallini16-Sep-14 5:13 
GeneralRe: Passing a class to a sprintf() and similar Pin
Richard MacCutchan16-Sep-14 5:30
mveRichard MacCutchan16-Sep-14 5:30 
GeneralRe: Passing a class to a sprintf() and similar Pin
jschell16-Sep-14 8:40
jschell16-Sep-14 8:40 
GeneralRe: Passing a class to a sprintf() and similar Pin
Albert Holguin16-Sep-14 8:42
professionalAlbert Holguin16-Sep-14 8:42 
GeneralRe: Passing a class to a sprintf() and similar Pin
jschell17-Sep-14 9:51
jschell17-Sep-14 9:51 
GeneralRe: Passing a class to a sprintf() and similar Pin
Crazy Joe Devola16-Sep-14 11:20
Crazy Joe Devola16-Sep-14 11:20 
QuestionI need your help Pin
Member 1107406614-Sep-14 16:07
Member 1107406614-Sep-14 16:07 
GeneralRe: I need your help Pin
CPallini14-Sep-14 18:46
mveCPallini14-Sep-14 18:46 
QuestionCan i trust the result of clock_getres? Pin
elelont212-Sep-14 1:34
elelont212-Sep-14 1:34 
AnswerRe: Can i trust the result of clock_getres? Pin
Stefan_Lang12-Sep-14 1:50
Stefan_Lang12-Sep-14 1:50 
AnswerRe: Can i trust the result of clock_getres? Pin
CPallini12-Sep-14 2:49
mveCPallini12-Sep-14 2:49 
Questionwhat is C language ? Pin
uetester111-Sep-14 22:01
uetester111-Sep-14 22:01 
AnswerRe: what is C language ? Pin
CPallini11-Sep-14 22:10
mveCPallini11-Sep-14 22:10 
GeneralRe: what is C language ? Pin
Albert Holguin16-Sep-14 8:31
professionalAlbert Holguin16-Sep-14 8:31 
QuestionInitializing structure with pointers to char arrays - bug Pin
Vaclav_11-Sep-14 4:59
Vaclav_11-Sep-14 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.