August 31, 2005

I just don't get it, what's with this->?

I'm looking at some code to help me understand how to use the dbghelp.dll Windows stack walking and symbol discovery stuff and the code I've found on the net uses a programming style that I've seen a few times before and have never understood the reason for.

if (this->m_sw->pSGSFA(this->m_hProcess, s.AddrPC.Offset, 
&(csEntry.offsetFromSmybol), pSym) != FALSE)
{
strcpy_s(csEntry.name, pSym->Name);
// UnDecorateSymbolName()
this->m_sw->pUDSN( pSym->Name,
csEntry.undName, STACKWALK_MAX_NAMELEN,
UNDNAME_NAME_ONLY );

this->m_sw->pUDSN( pSym->Name, csEntry.undFullName,
STACKWALK_MAX_NAMELEN,
UNDNAME_COMPLETE );
}
else
{
this->OnDbgHelpErr("SymGetSymFromAddr64", GetLastError(), s.AddrPC.Offset);
}

Why did the author feel it necessary to be explicit about his use of the current object by specifying this-> when accessing members? Where did this style come from and what purpose is it supposed to serve?


Share this entry: Email it! | bookmark it! | digg it! | reddit!

Posted by Len at August 31, 2005 09:29 AM | Comments (7) | Categories : Geek Speak
Comments

Many people advocate it as an alternative to Hungarian. Especially in the presence of auto-completion, this-> gives more focused help than just Ctrl+Space.

Personally, I hate it :-)

- Kim

Posted by: Kim Gräsman at August 31, 2005 09:45 AM

i suppose it's just a way of removing potential ambiguities, just as the global namespace prefix :: is used sometimes to make ensure the correct function lookup is found. i see that quite a lot, especially for ::SysAllocString for some reason. the only time i've actually seen a need for that is with a socket class that uses ::connect etc. when the class has its own connect function.

i don't normally use either myself, but sometimes if i'm lazy i will type this-> to see what the autocomplete brings up. i would always delete it once i've found what i'm looking for though.

i could kind of understand the reasoning for using it instead of hungarian but i wouldn't recommend it - and the example given doesn't do away with the pesky m_ anyway!

great site btw, fast becoming one of my favourites :)

Posted by: Ben at August 31, 2005 11:45 AM

Ben

I often use the global namespace prefix as "insurance against future change"... But to be honest it's just a habit I picked up a long time ago and with my current style of testing it's probably becoming unnecessary; in fact I can't remember when it's actually 'saved' me...

The this-> stuff just looks messy to me, especially with default syntax highlighting... I'd personally prefer a 'wart' on the variable (leading m_, trailing _ I don't really care as long as it's consistent).

Posted by: Len at August 31, 2005 11:54 AM

Sometimes "this->" is even necessary. With dependent
names and 2-phase lookup:

http://womble.decadentplace.org.uk/c++/template-faq.html#base-lookup


--
Vagn Johansen

Posted by: vj at August 31, 2005 04:02 PM

Interesting link, thanks.

In that situation I usually take one of the other routes; base@lt;T@gt;::base_func() etc...

Posted by: Len at August 31, 2005 06:33 PM

Just like to know whether you use external libraries specially 'BOOST' in your commercial projects and related pros-n-cons ?

Also i like to know your initial strategy of Software development. I mean you've written a lot about your coding and tsting techniques but i like to know how you start the project, whether you believe in UML and how you carry out analysis. If you've time then please write something about it here in the blog. Or if you've ever written anything abt it before then please give me the link ?

Posted by: ahsan Askari at September 1, 2005 08:06 AM

Ahsan, thanks for the questions. I've answered them in a new post here.

Posted by: Len at September 2, 2005 09:09 AM
Post a comment









Remember personal info?




Enter this code in the box below to prove that you're not some kind of automated spam robot...