|
Your example isn't be a problem since "test" is in the data segment and isn't going to change location. I would recommend returning a const char * , but that code is perfectly fine. What isn't fine is the following:
char* ToStr()
{
char buffer[5];
lstrcpy(buffer, "test");
return buffer;
}
|
|
|
|
|
If CString is the standard ATL/MFC string class, then there is nothing wrong with the code you've posted - ret will effectively be copied into val when the return statement is executed. Part of the implied contract of ToStr is that callers will pass the address of a CString object into which the return value can be copied.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: Part of the implied contract of ToStr is that callers will pass the address of a CString object
Not exactly - the original post shows:
CString ToStr()
{
CString ret = "test";
return ret;
}
void fun()
{
CString val = ToStr();
}
The contract here is that ToStr() will return a CString object, which the caller may store or destroy as he/she wishes. The code as written always returns a valid CString object so I don't see how it can fail. However function fun() does nothing with its CString val so that may be where the original problem occurred.
|
|
|
|
|
Note the use of the word 'implied'
An object (of struct/class type) gets returned from a function in C/C++ by the caller making space for it and the return statement in the function copying the actual value returned (ret in this case) into this space, constructing a temporary, unnamed object. This temporary object is then used in the callers context (in this case, it gets assigned to val).
A compiler is permitted to optimise away this temporary object when suitable, by giving the function a reference to the final destination (in this case val) rather than the temporary object.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Good point, I had not considered the full implications of optimisation.
|
|
|
|
|
<Slashdot style comment> Vote parent up!
|
|
|
|
|
I've to develop an user interface where buttons has
different font types and dimensions (different from
the default window type). I've seen that the CButtonST
class doesn't do that... and the CButton::SetFont
method called with the output of the CFont::CreatePointFont
doesn't work properly.
Does someone have some advices ?
Thanks a lot !
|
|
|
|
|
Something like this should work:
CFont f1;
LOGFONT lf1;
::ZeroMemory(&lf1, sizeof(lf1));
lf1.lfHeight = 14;
lf1.lfCharSet = ANSI_CHARSET;
lf1.lfWeight = FW_BOLD;
::lstrcpy(lf1.lfFaceName, "Arial");
f1.CreateFontIndirect(&lf1);
GetDlgItem(IDC_BUTTON)->SetFont(&f1);
f1.Detach();
Change lfHeight, lfCharSet and lfWeight to your liking.
|
|
|
|
|
Hi,
I would like to change the appearance of tabs in a multiline edit control, all tabs should have the same width of 8 pixels. MSDN[^] talks about dialog units, where I would like to specify pixels. The following line sets the distance to "something" depending on the font size of the edit control:
int nTabWidth = 8; m_edit.SetTabStops(nTabWidth);
::SendMessage(m_edit.m_hWnd, EM_SETTABSTOPS, 1, (LPARAM)(LPINT)&nTabWidth);
How to calculate the correct value? Thanks for any help.
|
|
|
|
|
Moak wrote: How to calculate the correct value?
Not sure about this, but it seems to work...
int nTabWidthInDialogUnits = ::MulDiv(nTabWidth, 4, LOWORD(::GetDialogBaseUnits()));
|
|
|
|
|
EM_SETTABSTOPS message gets tabswidth in dialog units, which equal to: 1 character = 4 dialog units width / 8 dialog units height (independent of the selected font).
As you can see in Remarks section from http://msdn.microsoft.com/en-us/library/bb761663%28VS.85%29.aspx[^], to convert measurements from dialog template units to screen units (pixels), use the MapDialogRect function.
Hope this helps.
|
|
|
|
|
There are many products publicied and which their authors proudly specify the langage/technology in which they are written.
In order to avoid to cite particular site : I can read things like :
"Written in .Net"
"Pure Managed"
"Full Web"
There are many labels and they are as if to serv the good image of the application.
Now, can I do that for a C++ program ?
Unmanaged high performance C++
Do you have other suggestions ?
What about the icon, what can I put ( C++)
Easy Profiler : a compile-time profiler for C++
www.potatosoftware.com
|
|
|
|
|
what about
Pure 100% unmanaged high speed Potato++ software
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Potato software wrote: Now, can I do that for a C++ program ?
Unmanaged high performance C++ Laugh
Advertise as following...
No Java/Flash/Ajax/.Net/Silverlight/PHP/Perl/Python required.
Optionally with:
Follow us on Youtube/Twitter/Facebook/Lolcats.
Webchat in Europe
No Java/Flash/Ajax/.Net/Silverlight/PHP/Perl/Python required.
Follow us on Youtube/Twitter/Facebook/Lolcats.
|
|
|
|
|
Easy Profiler : a compile-time profiler for C++
www.potatosoftware.com
|
|
|
|
|
Potato Software
Performance-wise only one step short of hand-typed machine code
|
|
|
|
|
Hi
I am useing ODBC 5.1 Driver for connect to MySQL.It's working fine for XP OS.I am getting error on Window Server.on server OS i an able to connect but after connection i am getting error.
Can't create an instance of Command
code
CString por="Driver={MySQL ODBC 5.1 Driver};";
CString Database="Database="";";
CString User="User=";
CString Userp=root;
CString password=";Password=";
CString Passwords=root;
CString option=";Option=3;";
CString final=por;
final +=Database;
final +=User;
final +=Userp;
final +=password;
final +=Passwords;
final +=option;
CoInitialize (NULL);
_bstr_t strCon(final);
try
{
HRESULT hr = m_pConn.CreateInstance (__uuidof (Connection));
if (FAILED (hr))
{
}
if (FAILED (m_pConn->Open (strCon,bstrString,bstrString1,0)))
{
AfxMessageBox ("Can't open datasource");
}
GetDlgItemText(IDC_EDIT4,m_cbrow);
GetDlgItemText(IDC_EDIT1,m_host);
if(m_host=="localhost")
{
}
else
{
MessageBox("There is an error to connect with MySQL. Please check your input parameters or server configuration.","Message",MB_ICONEXCLAMATION);
return;
}
AfxMessageBox ("Connection successfully");
_CommandPtr pCommand;
hr = pCommand.CreateInstance (__uuidof (Command));
<code>"Erro showing here"</code>
if (FAILED (hr))
{
AfxMessageBox ("Can't create an instance of Command");
return;
}
::_RecordsetPtr pRecordset;
pCommand->ActiveConnection = m_pConn;
Plz help me.
|
|
|
|
|
|
I have no idea.Plz help me
|
|
|
|
|
Hi All
How can i ReversFind and count number of words?I have CString name="agsdfgggggg" how can count and cut these part "gggggg" from name.
I mean i want to show this CString name="agsdfgggggg" to CString name="agsdf".
Can any one help me.
|
|
|
|
|
Is "agsdfgggggg" a unique string? Is there some pattern? Does it always have the same number of characters?
If you give us a bit more information we may be able to give you better advice than CString::Left(6) for example.
|
|
|
|
|
|
1 2 3 4 5 6
Sorry, couldn't resist...
|
|
|
|
|
I don't blame you.
|
|
|
|
|
Wow, I have no clue what you are talking about but the answer is probably liquid nitrogen[^]
Ok, serriously now. What does ReverseFind have to do with your question ? Why are you talking about words (I only see letters in your string) ? Is your string always "agsdfgggggg" and you always need to remove "gggggg" ? In which case, I don't see why you are asking the question
|
|
|
|