Today in the use of C++\CLI encountered a point on the delegation, callback use of problems, simply record
First, paste the code in simple C # using System.Threading.Timer.
Timer gametimer=new Timer (new TimerCallback (this). Ongametimer), null,0,500);
The above timer will call the Ongametimer method every 500 milliseconds, the Ongametimer method is defined as follows:
void Ongametimer (Object obj) {...}
Based on the above code, combined with the use of C #, we write the C++\CLI code.
timer^ gametimer=gcnew Timer (gcnew TimerCallback (This->ongametimer), null,500);
void Ongametimer (object^ obj) {...}
Well, vs doesn't prompt for any syntax errors, then what about the results of the compilation?
Error120error C3867: ' Gameserver::frmmain::ongametimer ': function call missing argument list; Using ' &gameserver::frmmain:ongametimer ' to create a pointer to member follow the prompts, it requires a pointer to a method function, and also gives the code "& ...", modified as follows: timer^ gametimer=gcnew Timer (gcnew TimerCallback (&frmmain::ongametimer), null,500); But this time has not compiled, the prompt syntax error, the compiler gives the error message is: Error120error C3352: ' void Gameserver::frmmain::ongametimer (system::object ^) ': The specified function does not match the delegate type ' void (system::object ^) ' This error hint is really confusing to the mind, the specified method function and Dele Gate delegate does not match.
Okay, a search, the MSDN sample code is linked as follows: Http://msdn.microsoft.com/zh-tw/beginner/c320cx3h (ZH-CN). aspx after repeatedly flipping through the code several times before finally figuring out the correct wording, as follows: timer^ gametimer=gcnew Timer (gcnew TimerCallback ( This, &frmmain::ongametimer), null,500); Later only to see that it really does not add this parameter when the syntax error is accurate, and error invalid delegate Initalizer-function does not match the delegate type. C++\CLI, has not been to system learning, just happen to use C #, and understand some C + +, directly used, but the process is often encountered some very basic problems, but it is these basic problems stuck to themselves. It's a mix of C + + and. NET, and it may be awkward to write code alone from that habit, perhaps as a new programming language, and these strange local laws are not surprising. About it, if you want to enjoy. NET fast, and do not want to abandon the previous C + + existing things, or need to deal with some of the underlying interface, then it is worth to hug!
Off Topic:
Recently embarked on rewriting a very old game server, currently using C # in accordance with the original C + + code rewrite Gateserver,loginserver,roleserver,guildserver, but gameserver choose to use c++\ The CLI is refactored. This code is estimated to be born in about 1998 years, not using any design patterns, even a lot of the same code is not a simple package, gameserver in the main logic is written in the Game.cpp, the CPP is full of 2M more, 7-8w lines of code are rubbed together. If Gameserver also uses C # rewrite, then it is impossible to predict the workload. Fortunately, the existence of the CLI, so that the large and messy C + + code does not take much effort to be. NET, tested run into the game all normal. The rest of the network communication of its broken with my C # write Xksocket replaced, it is not a problem! Xksocket git address: http://git.oschina.net/dreamzgj/XKSocket
C++\CLI using. NET delegation, *callback note "This"