4
votes

My Environment:

OS - Windows7 Pro(32bit)

IDE - RadStudio XE2 Update4

I am wondering about Synchronize() function.

The Synchronize() function is used in the thread program. About using the Synchronize() in C+ builder, the example is as follows ( as can be seen in here)

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(&UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TMyThreadClass::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }

What I am confused is that , in some older version of C++ builder (e.g. bcb6), the Synchronize() was used

// Synchronize(UpdateCaption);

without "&" before the function name;

Also in the delphi code as in here, there is no "&" before the function name;

// Synchronize(UpdateCaption);  

So, which is the correct way to use Synchronize() in C++ builder

// Synchronize(UpdateCaption);  

or

// Synchronize(&UpdateCaption);  

I tried both in the actual code, but seems identical in the working manner. Do both UpdateCaption and &UpdateCaption return address of functions?

1
in Standard C++, UpdateCaption and &UpdateCaption are illegal. It's a C++Builder extension that, for a member function, &UpdateCaption evaluates to a __closure, which is a pointer to the combination of an object and a member function on that object. (C++11 added closures to the language; however C++Builder predates that by a long way, so they had to add their own extension for it). Apparently there is also a feature that UpdateCaption implicitly converts to &UpdateCaption in overload resolution, although it doesn't do so on its own! (e.g. UpdateCaption; gives an error). - M.M
@MattMcNabb: Thank you for your comment. So this is extention by C++ Builder not the standard C++. - sevenOfNine
Yes - standard C++ (before C++11) does not have closures ; the only functions you can take the address of are non-member functions. - M.M
@MattMcNabb: That is not true. Standard C++ has had Member Function Pointers for years, so you can take the address of a member function. It is just very limited in use. Synchronize() (and events) specifically rely on Borland's __closure extension, which is much more flexible than a MFP. A MFP is specific to a particular class. A closure is not, which makes it possible to assign different classes to the same pointer-to-member variable. - Remy Lebeau
@RemyLebeau I mean that &FunctionName does not work in Standard C++. To get a MFP you have to write &ClassName::FunctionName. - M.M

1 Answers

5
votes

So, which is the correct way to use Synchronize() in C++ builder

// Synchronize(UpdateCaption);

or

// Synchronize(&UpdateCaption);

They both work, but & is preferred.

Do both UpdateCaption and &UpdateCaption return address of functions?

Yes. If you refer to a function/method without specifying parenthesis for the parameter list, the address of the function/method is assumed. The & just makes it more explicit.