|
Option 1 is the generally accepted method style, unless B needs to return a status value like:
BOOL B( CString &out_string );
Use the best guess
|
|
|
|
|
Any decent compiler with return value optimization will compile about the same machine code from the above method declarations so I would use CString B(); because it looks nicer. The second version seems much like "human optimized" code but its not that terrible. From this example what came to my mind is the usual CString ToString(); method. Its easier to use the first declaration because the second always requires you to declare variables on the stack before the call while the first one allows you to write nicer code, for example you can immediately pass the return value of B() to another function call that receives a string.
Another thing you need to know here: Even if there is no return value optimization in your compiler (Visual C++ has it) CString is a reference counted string that means: It is just a "pointer" to the string data so copying it copies only a pointer and increases then decreases a refcount of the string data when the returned temp refcount-pointer object (CString) is deleted.
|
|
|
|
|
Am I correct in thinking that "return value optimization" self descriptive rather than having a special meaning?
In the first call B, more of a function style, is written as:
CString B( ){ return internal_string }
While the second, more of a procedure style, would look like:
void B( CSrting &some_string );<br />
{ some_string.empty();<br />
some_string = internal_string;<br />
}
That presumes that some_string = internal_string is a copy operation and not a reference operation. Is that presumption true or false?
The first hands out an address within B's space while the second does not. That also means that the caller would have to copy it before doing anything that would change it. I know we can adorn the return value with "const", but still.
Is that logic flawed?
Thanks for your time
If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig_106/
|
|
|
|
|
If you are interested about how optimizations work please use google. You can never expect a compiler to optimize your code at a particular code location but please don't write ugly code just to try to optimize out things prematurely. Whether to make a piece of code manually-optimized and ugly depends on a few factors: The size of the data structure you want to return, the frequency with which the function is being called,... Keep the code pretty and easy to read and optimize/uglify the critical often-executed parts. Optimization takes place periodically during development. While optimizing you find out what part of the code takes most of the execution time and you cut it off here and there. Often these codepieces are found at the most unexpected places and the biggest performance degradations happen due to algorithmic problems.
|
|
|
|
|
Your analysis is a bit flawed.
First of all, you don't need to empty the string before populating it:
bkelly13 wrote:
<br />
void B( CString &some_string ); {<br />
some_string.empty(); some_string = internal_string;<br />
}<br/><br />
There are several things that happen in this (fixed) code that don't happen in the 'first call' version. When calling the API, there is more setup on the part of the caller. A CString variable needs to exist already, and API setup makes sure that it is either empty or ref count is zero (and writable). Assuming that internal_string is another CString, no copying takes place. The some_string object's header info is updated to duplicate the same info in internal_string, and the ref count is incremented. If internal_string is 1MB in size, only a pointer and counter are changed (ie. no copying).
Again, if internal_string is a CString, a similar process also happens in your 'first call' code (ie. no copying).
If internal_string is a (const?) char* or (const?) char[] or something else that looks like one of those, then a copy needs to take place for both versions of the code.
Windows 8 is the resurrected version of Microsoft Bob. The only thing missing is the Fisher-Price logo.
- Harvey
|
|
|
|
|
Option 2 can be preferred for object return.
Option 1 is usually used to return primitive types.
|
|
|
|
|
It depends on few things. Most importantly what bussiness logic is there in B.
For example suppose B function is responsible to read data from a very large file and you want to return that from B. There are changes that file open error may occure then:
int B(CString &out_string) will be best option because of two reasons.
1. The error code can be returned as return parameter
2. You dont need to accumulate the content of file in a local variable which gets added and deleted on stack while transferring that as return value.
Manoj
Never Gives up
|
|
|
|
|
I want to change one folder's icon.How can i do it with shell extension or other ways but except using desktop.ini file.
|
|
|
|
|
Hi guys,
During developing on windows I had to mess with COM a bit.
So I want to know,
How common is the use of COM and ATL?
Are these technologies getting old and only supported by other apps, or there are new software built with these tech?
Thanks!
|
|
|
|
|
marisha.u wrote: How common is the use of COM and ATL? How could we tell without running a survey?
Use the best guess
|
|
|
|
|
Microsoft products still make use of these. If you are developing something that is interconnected for example with office products than chances are good that you have to mess with COM as some programming interfaces of these products are exposed with COM. Sometimes interconnection between 2 softwares written in different languages is the easiest via COM if both language compilers have COM support (this is also dependent on the kind of interconnection - its quite rare when a pipe or loopback socket isn't enough). In my opinion if you are developing your own software then the best is to avoid COM if possible. Keep it simple. Use a technology/library only if it makes your job easier or if it is explicitly requested by your client. Don't use something "just because" its a fashion or because it is used by another product. It can be possible that a famous product uses technology X but the architects are cursing the day of choosing that technology and today they simply cant afford (time/$) getting rid of it. Before you start using something do some research and compare different solutions on a simple example program. Investing the time in this pays off in the long run especially in case of a bigger project.
|
|
|
|
|
You described exactly my situation
I had to mess with COM because of historical reasons, and because I'm working with IE and Windows.
I just wondering how useful it is, and should I use it more in the future?
|
|
|
|
|
I think the answer is in my previous post. Use it if you have to and you don't have a better/easier solution.
|
|
|
|
|
I think that these technologies still using by Microsoft and I think also that they are very important.
but it's not wrong to use a new software builder and coder , they all the same but the new are easier to use.
|
|
|
|
|
Dear friend,
Iam currently developing window application using VC6 and VC2008 and interested to learn ATL, STL, WTL and COM. I have refered many article and tutorial in the net. But I felt difficult to understand the concepts and explanation. Friends kindly suggest me any material or site which many helpful to learn it from beginning.
Thanks and Regards,
S.Shanmuga Raja
|
|
|
|
|
Introduction to COM - What It Is and How to Use It.[^] by Michael Dunn, is an excellent introdiuction, as are his other articles on the subject. Scott Meyers has written some useful books on STL which you can probably find on Amazon. I don't use ATL/WTL so cannot offer suggestions for those. However, I would say that if you find these subjects difficult to understand then it is important to focus on only one at a time.
Use the best guess
|
|
|
|
|
Inside COM by Dale Rogerson (very good intro)
Professional COM Applications with ATL by Sing Li and Panos Economopoulos (more advanced)
COM IDL & Interface Design by Al Major (everything you ever wanted to know about interfaces)
Of course they're all a bit dated...
|
|
|
|
|
hi,
i will suggest to start with Inside COM by Dale Rogerson and then start with ATL/WTL
for STL - start with Nicolai M. Josuttis books/online resources.
regards,
Ajay
|
|
|
|
|
I am trying to use WTL but i need the ATL headers.
I install vs 2012 express and the SDK v7.1.
but I still cannot find the ATL headers.
like: atlbase.h
what I have to do?
and , is the ATL still included in some microsoft packages for free?
|
|
|
|
|
I t6hink you have to pay for one of the full Visual Studio editions, same as MFC.
Use the best guess
|
|
|
|
|
Hi all,
When using jscipt9 (Chakra) from C++ with the AddNamedItem() / GetScriptDispatch() functionality to create a module-like behaviour (context separation) I get E_OUTOFMEMORY from GetScriptDispatch() .
This happens only when omitting the SCRIPTITEM_GLOBALMEMBERS flag with AddNamedItem() , no matter what other flags are given.
I tried already everything, including different flags (all possible flags, not only the known ones), changing the order of calls to IActiveScript , calling it in different states of the script engine, invoking different versions via SCRIPTPROP_INVOKEVERSIONING , implementing all requested interfaces in my IActiveScriptSite etc - no result.
For older versions (means: not using jscript9) of jscript everything works as expected. But as soon as I switch the CLSID to Chakra it breaks.
I posted already on msdn: "AddNamedItem() / GetScriptDispatch() broken in jscript9?" [^] and filed a bug report [^].
You can reproduce with a most simple sample I put on github: https://github.com/IUnknown68/ascptest [^]
My main questions is: Am I doing something wrong? Is there something undocumented I have to do? Or is it really a bug?
Thanks,
imagiro
Edit: Run the sample from the command line with 9 as argument to invoke chakra. Otherwise the old jscript engine will be used.
modified 4-Jul-13 7:03am.
|
|
|
|
|
Hi, I have a critical problem, please help.
I'm developing a web game, which uses ATL based ActiveX control to display the 3d game world. When the game activex control was loaded by IE, a seperate game thread was created and the game HWND was created in the thread as child window of the activeX window. Then a message loop was running in the thread to process game logic, rendering,..etc.
In the game thread, I created a windowless mode Shockwave Flash player COM interface to display 2D content, which uses OleDraw to render the image to a texture.
Ok, All these things runs fine in IETab on chrome, BUT not in the IE! The flash player displays nothing in IE! In fact, some web browser which used IE core does work correctly, except IE!
So what could be the reason, and how can I fix this problem?
BTW: Sorry, my english is not good
|
|
|
|
|
Hi Guys,
Reading about COM Threading since two days..
But still i Couldnt understand Exactly " What is an Apartment ".
Kindly can any one explain in simple terms or show some source to understand more.
Thanks,
TV Krishna Rayalu
|
|
|
|
|
A good description can be found on this MSDN page[^]. Basically it is whether the COM object can support multi-threading or not: crazy Microsoft terminology again.
Use the best guess
|
|
|
|
|
Thanks Richard.
That link is informative.
i could able to understand STA and MTA, i got more discription
on what STA and MTA do then what exactly Apartment is.
Can you please define it in simple words.
Thanks & Regards,
TV Krishna Rayalu
|
|
|
|