|
hrishiS wrote: But could you please differentiate these with respect to shallow copy and deep copy?...and why we say so?
See here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi,
In the below code ret value become invalid as it comes out of the funcation scope. can any suggest better idea to solve this.
CString ToStr()
{
CString ret = "test";
return ret;
}
void fun()
{
CString val = ToStr(); // it returns nothing (bad pointer)
}
I have compiled this code in MS VC++2005 under winxp OS.
Thanks,
Nandu
|
|
|
|
|
void ToStr(CString& val)
{
val = "test";
}
void fun()
{
CString val;
ToStr(val);
}
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().
Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr
-----------------------------
I am a beginner
|
|
|
|
|
hrishiS wrote: the problem I could notice is that, ret is a automatic variable , so it is no longer available once it comes out from the function ToStr().
A copy of the object will be returned, so no there's no problem there.
hrishiS wrote: Either you can have it as global variable, or make a variable of pointer/reference of String , inside fun and pass it to ToStr
1) global variable for that is really ugly (but that's my personal taste)
2) variable of type pointer: not really nice neither because each time you will create the object on the stack and furthermore, you delegate the destruction of the pointer to somewhere else, which potentially introduce memory leaks.
3) return a reference to the object: that's wrong. In this case, the object will be destroyed, no copy will be made and you will return the address of the destroyed object which is wrong.
So in conclusion, returning the object as he mentioned is the best option.
I could agree with superman in the sense that his proposed solution is more efficient: no temporary object gets created but honestly, it doesn't make a lot of difference (unless your code is in a part of the code which should be highly optimized).
|
|
|
|
|
thanks for all the points... I can understand How much I have to learn more in deep ...And sorry to the real sender of the question... pls listen/take advice from the the seniors...I am too a learner
-----------------------------
I am a beginner
|
|
|
|
|
There's no problem with your code. The problem appears when you return something like this:
char* ToStr()
{
return "test";
}
In which case you return the address of a temporary variable. If you return an object, the compiler will make a copy of it and assign it to val correctly.
EDIT: changed the return type to char* instead of CString.
modified on Tuesday, September 8, 2009 9:54 AM
|
|
|
|
|
You sure about that Cedric?
I think you'll find that "test" is used to construct a CString object that's returned by copy...that's what happened when I compiled and debugged it, anyway
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Ooops, sorry,I forgot to change the return type to char*
If the return type is CString, then I agree with you, there's no problem neither.
Thanks for pointing it out.
|
|
|
|
|
I don't find any issues with your code. You are returning by value and not returning any reference to the temporary. So I guess your code is perfectly valid.
Cedric Moonen wrote: If you return an object, the compiler will make a copy of it and assign it to val correctly.
Even if you return by value, with return value optimization, a copy won't be made.
Please correct me if I am wrong here.
[edit]Saw your post updated[/edit]
|
|
|
|
|
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
|
|
|
|