|
I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!
...
Interger a(i + rv.i);
return a;
...
Its output was
constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)
...
return Integer(i + rv.i);
...
Its output was
constructor -> destructor
I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???
|
|
|
|
|
In the first case, the compiler creates a temporary object on the stack (in the function, object a). You return this object by value, so the compiler creates another temporary object on the stack (the return value) and then calls the copy constructor to copy object a into the temporary object. Than, object a gets destroyed and after finishing the statement, the temporary return value gets destroyed also.
In the second case, you introduce no temoray variable in the function itself. You call the contructor to return the value. In this case, the destructor of the returned temporary variable is called.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Bob Stanneveld wrote:
In the second case, you introduce no temoray variable in the function itself.
in fact, it is done implicitely, but a tempoary object is still created...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
by making return Integer(i + rv.i); the compiler creates a temporary object which is initalized by its constructor (called here with the parameter i + rv.i).
then, by executing return the program exits the local scope of the function and so calls the destructors of the local variables...
nothing more, nothing less.
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Actually, there is no real diference. Do whatever is clearer to you....
Any decent compiler is intelligent enough to optimise away the temporary variables. The compiler does a much better job at low-level optimisation than any programmer, I usually leave this job to it and keep the code the clearest for me.
My 2 cents..
Guy
|
|
|
|
|
no real difference. But depending on the return type of the function, the second bit of code could create another temporary.
For instance:
int function()<br />
{<br />
return Integer(i + rv.i);<br />
}
and
int function()<br />
{<br />
Integer a(i + rv.i);<br />
return a;<br />
}
will both create a temporary object (even 'a' could be considered temporary) and return the encapsulated int value.
While
Integer function()<br />
{<br />
return Integer(i + rv.i);<br />
}
and
Integer function()<br />
{<br />
Integer a(i + rv.i);<br />
return a;<br />
}
Will both create TWO temporary objects, one inside the function, and one as the return value.
|
|
|
|
|
|
|
Anonymous wrote:
dfgdfgdf
Are you learning ABCD......
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Yes, you ARE a moron. Next...
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
very funny
V
|
|
|
|
|
My problem is that i need to do some addition, subtraction, and multiplication on a number and then display the result in a textbox or label or something like that so that it is displayed in the window. I have been trying to find how to convert from an int to a system::string (if it is even possible) but could not find anything. Am i going about this wrong just cannot figure out how to do it correctly. I am using microsoft visual c++ express edition 2005 beta 2.
I have tried searching the web for anything like this and could not find anything that would work. I have been working on this for over a week now and connot figure it out. I am new to programming so it is probably pretty simple but i cant find any help on this. Any help that anyone could offer would be greatly appreciated.
-Kyle
|
|
|
|
|
system::string means you're using managed C++, not normal C++ ? system::string has a Format method to do what you want. If you were using C++, then istringstream or *shudder* sprintf would convert int to string, or CString::Format ( from memory ).
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
i am using managed c++. if you could include a sample code of how to use the System::String Format function it would help out greatly. i looked at it but it asked for a System::String and a System::Object what would i put for the each and where does the int variable go?
if i use a textbox named txt and an int variable named num, would it be:
txt->Text->Format(txt->Text, num);
is this right or am i way off??
thank you for the help.
-Kyle
|
|
|
|
|
The system::string is the format string, and the object is the object to format. You can pass in as many objects as you like, they are numbered from 0.
txt->Text = System::String::Format("{0}", num);
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I tried that a few different ways some ways compiled but did not do what i wanted it to and the way that was posted gives me the following error:
error C2664: 'System::String ^System::String::Format(System::String ^,System::Object ^)' : cannot convert parameter 1 from 'int' to 'System::String ^'<br />
1> No user-defined-conversion operator available, or<br />
1> No standard conversion exists from the boxed form of the arithmetic type to the target type
and would not compile.
i searched the web and found the stringbuilder class and was wondering if this would do what i wanted it to.
- Kyle
|
|
|
|
|
A string builder will do what you want, but it's overkill.
The basic problem is that MC++ is a crappy kludge, I'm glad I've never used it in depth. You need to manually box your integer for string::format to work. You do it like this:
int n = 2003;
Console::WriteLine(System::String::Format("{0}", __box(n)));
That is, this is an example of how to box your integer.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
if i try this it gives me the following error:
error C2227: left of '->ToString' must point to class/struct/union/generic type<br />
1> type is 'int'
- Kyle
|
|
|
|
|
|
Thank you Jose Lamas Rios. The num.ToString() worked.
I had been tring for quite some time to figure that out thank you very much.
- Kyle
|
|
|
|
|
|
i have sdk netmeeting3, i want to edit its. but i can't edit its with class wizart on VC++, can i?
|
|
|
|
|
You cannot edit the SDK, no. You can merely use the headers and lib files to link to it, so your code can call it.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I am learning Visual C++.net and have found that WinExec is no longer used/supported. I am trying to create a Windows Form employing a combo box. I want the user to select an item from the drop down list and have them run a program when the run button is clicked. How do I get The SelectedIndexChanged event to fire whenever the user selects a new program? Thanks for your inputs.
RRL
|
|
|
|