CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Simple as hell

  1. #1
    Join Date
    May 1999
    Posts
    19

    Simple as hell

    Hello.

    Here is a very simple question. My function is defined as:
    char * myfunc (void);

    My function prepares some text and want to pass it. I would like at first to send the whole text, so I have the possibilites:
    1. static char szText [1000] and then return the pointer.
    2. define char szText [1000] globally.
    3. use CString object.

    I wouldn't like to use CString and I guess there is some trick programs use in this case. The best solution I have in the list is to use static, but I don't want it to take the memory forever. I don't want to use calloc in one function and free in another as well.



  2. #2
    Guest

    Re: Simple as hell

    OR use new ro malloc to create a new buffer and make sure that the calling function deletes it when finished. Not generally considered a good practice but it works. I don't know why you don't want to use CSting though...they are very simple to use and reasonably efficient (not great but O.K.).


  3. #3
    Guest

    Re: Simple as hell

    Use CString


  4. #4
    Guest

    Re: Simple as hell

    It would be easiest to pass a CString object to:

    void myfunc (CString sMyString) // sMyString may grow in size with no problem

    Next place the following code fragment in the calling function:

    CString sMyString;
    myfunc(sMyString); // May cast sMyString into LPTSTR if you want. Read document

    CString object is often more convenient than char *. It will be destroyed automatically as it goes out of scope.



  5. #5
    Join Date
    May 1999
    Posts
    8

    Re: Simple as hell


    void myfunc (const CString& InString)
    {

    // do processing here

    return OutString;
    }



  6. #6
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: Hi, Sally. Your code is wrong.

    Hi, Sally and Sue.

    Your code is wrong.

    void myfunc (const CString& InString)
    {
    // do processing here
    return OutString;
    }

    1) If you want to return CString,
    CString myfunc(CStrig str)
    {
    ......
    return OutString;
    }

    Or use reference.
    void myfunc(CString& InString)
    {
    CString str("Aha");
    InString += str;
    }
    //In this case, InString is updated without return.
    That is,
    CString str("First ");
    object.myfunc(str);
    //now str is "First Aha"

    If I have some errors, please point it out.
    Regards.
    -Masaaki Onishi-










  7. #7
    Join Date
    May 1999
    Posts
    8

    Re: Hi, Sally. Your code is wrong.

    CString myfunc(const CString& inString)
    {
    CString outString;
    // do processing here
    // like outString = inString + "extra";

    return outString;
    }

    this will work beautifully

    Sally


  8. #8
    Join Date
    Apr 1999
    Posts
    383

    Re: Hi, Sally. Your code is wrong.

    Indeed it will. It was your original post that had a void function returning a CString that won't work... :-)

    Dave


  9. #9
    Guest

    Re: Simple as hell

    This may be one solution


    // myclass.h

    class CMyClass
    {
    public:
    CMyClass();
    virtual ~CMyClass();
    void CMyAlloc();
    CString* m_szText;
    };


    // myclass.cpp

    CMyClass::CMyClass()
    {
    m_szText = NULL;
    }

    CMyClass::~CMyClass()
    {
    if(m_szText != NULL) // Don't delete if not allocated
    {
    delete m_szText;
    m_szText = NULL;
    }
    }

    CMyClass::CMyAlloc()
    {
    if(m_szText != NULL) // Don't allocat if already allocated
    {
    m_szText = new CString;
    m_szText->Format(_T("%s"), _T("My String"))
    }
    }


  10. #10

    Re: Simple as hell

    Use CString. CString is always at least as efficient as char arrays, and often *more* efficient. Anything you can do with char arrays, you can do with a CString.

    CString sMyData;
    sMyData = "Lots of data";
    DoSomething(sMyData);

    // If DoSomething does not modify "sMyData"...
    void MyClass:oSomething(const CString& sMyData)
    {
    }

    // If DoSomething does modify "sMyData"...
    void MyClass:oSomething(CString& sMyData)
    {
    }

    Also, buy a *good* C++ book like "Thinking in C++" or "Effective C++"...

    HTH.

    LA Leonard - Definitive Solutions, Inc.

  11. #11
    Join Date
    May 1999
    Posts
    19

    Re: Simple as hell

    How do I use CString in non-mfc appl?
    For example: I'm writing a console appl. I have used #include <windows.h>, then there is another header I have to include ~<afx..h>. When I include it it stats it won't work with <windows.h> included. I didn't understand the idea, so I decided not to use CString, which I generally like and use.

    Futhermore, If I would like to pass an actual test and not a pointer, how do I do that?

    string myfunc ();

    string myfunc ()
    {
    char szBuff[1000];
    return (szBuff);
    }
    How do I get the text, if I don't know the that szBuff is actually [1000]?



  12. #12
    Join Date
    May 1999
    Posts
    19

    Re: Simple as hell

    How do I use CString in non-mfc appl?
    For example: I'm writing a console appl. I have used #include <windows.h>, then there is another header I have to include ~<afx..h>. When I include it it stats it won't work with <windows.h> included. I didn't understand the idea, so I decided not to use CString, which I generally like and use.

    Futhermore, If I would like to pass an actual test and not a pointer, how do I do that?

    string myfunc ();

    string myfunc ()
    {
    char szBuff[1000];
    return (szBuff);
    }
    How do I get the text, if I don't know the that szBuff is actually [1000]?

    btw: without using destructors/constructor clases.


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