Click to See Complete Forum and Search --> : Simple as hell
Ran
April 8th, 1999, 06:21 PM
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.
April 8th, 1999, 06:28 PM
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.).
April 8th, 1999, 06:43 PM
Use CString
April 8th, 1999, 07:34 PM
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.
Torkel
April 8th, 1999, 08:07 PM
void myfunc (const CString& InString)
{
// do processing here
return OutString;
}
Masaaki
April 8th, 1999, 10:48 PM
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-
Torkel
April 8th, 1999, 11:32 PM
CString myfunc(const CString& inString)
{
CString outString;
// do processing here
// like outString = inString + "extra";
return outString;
}
this will work beautifully
Sally
Dave Lorde
April 9th, 1999, 05:24 AM
Indeed it will. It was your original post that had a void function returning a CString that won't work... :-)
Dave
April 9th, 1999, 07:58 AM
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"))
}
}
LALeonard
April 9th, 1999, 09:23 AM
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::DoSomething(const CString& sMyData)
{
}
// If DoSomething does modify "sMyData"...
void MyClass::DoSomething(CString& sMyData)
{
}
Also, buy a *good* C++ book like "Thinking in C++" or "Effective C++"...
HTH.
LA Leonard - Definitive Solutions, Inc.
Ran
April 9th, 1999, 05:55 PM
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]?
Ran
April 9th, 1999, 07:17 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.