Board index » cppbuilder » ternary operator ?: used with member function pointers

ternary operator ?: used with member function pointers


2007-06-30 04:10:14 AM
cppbuilder86
Previously defined stuff:
// an action list containing the action:
TAction *ActionMakeDistribFiles;
// an event handler for OnUpdate of the action:
void __fastcall ActionMakeDistribFilesUpdate(TObject *Sender);
bool EnableActions;
** Q: Why with this construct do I get a compiler error:
ActionMakeDistribFiles->OnUpdate = EnableActions ?
ActionMakeDistribFilesUpdate : NULL;
[C++ Error] MainForm.cpp(3945): E2235 Member function must be called or its
address taken
BUT when I simply use If/else:
if (EnableActions)
ActionMakeDistribFiles->OnUpdate = ActionMakeDistribFilesUpdate;
else
ActionMakeDistribFiles->OnUpdate = NULL;
it compiles and works fine.
 
 

Re:ternary operator ?: used with member function pointers

"static array?" < XXXX@XXXXX.COM >writes:
Quote
// an action list containing the action:
TAction *ActionMakeDistribFiles;

// an event handler for OnUpdate of the action:
void __fastcall ActionMakeDistribFilesUpdate(TObject *Sender);

bool EnableActions;

** Q: Why with this construct do I get a compiler error:

ActionMakeDistribFiles->OnUpdate = EnableActions ?
ActionMakeDistribFilesUpdate : NULL;

[C++ Error] MainForm.cpp(3945): E2235 Member function must be called or its
address taken
Please post the minimal (i.e. just enough, not more and not less) code
that causes the compiler to produce this message.
 

Re:ternary operator ?: used with member function pointers

Closures are a C++Builder concept that do not work in all areas of the C++
language. The ternary conditional operator is one example of where things
don't work completely. Use the if...else construct as you are already
doing.
Quote
ActionMakeDistribFiles->OnUpdate = ActionMakeDistribFilesUpdate;
BTW, you should probably get in the habit of taking the address of the
method:
ActionMakeDistribFiles->OnUpdate = &ActionMakeDistribFilesUpdate;
This has been the prefered syntax for years and is mandated by the compiler
with BDS2006 and up.
- Clayton
 

{smallsort}

Re:ternary operator ?: used with member function pointers

Clayton Arends wrote:
Quote
This has been the prefered syntax for years and is mandated by the compiler
with BDS2006 and up.

Well, in this case I'd suggest using the correct syntax and use
&Class::method :)
 

Re:ternary operator ?: used with member function pointers

"static array?" < XXXX@XXXXX.COM >writes:
Quote
** Q: Why with this construct do I get a compiler error:

ActionMakeDistribFiles->OnUpdate = EnableActions ?
ActionMakeDistribFilesUpdate : NULL;

[C++ Error] MainForm.cpp(3945): E2235 Member function must be called or its
address taken
First, be aware that __closure objects (the thing a TAction holds) is
not part of C++ at all, and is purely a Borland/CodeGear extension to
the language for Delphi compatibility. As such, no rules in the
language dictate what should happen when you use them.
As is common with other Delphi-C++ object model mergers, it works "for
the most part" but there tends to be some rough edges, and I think
you've stumbled across one.
The ternary operator forms an expression, which always has a value of
a single "type", and that type must be determined at compile time by
picking between the two expressions in the result of the operator. In
this case, you're passing a member function name, which by itself is
not a vaild expression.
However, when you assign a member function name to a __closure, BCB
does its magic and converts it to a __closure. I think that their
language extension is not fully integrated into the language.
For true C++ member function pointers (which are distinctly different
from __closures), the syntax REQUIRES that you use the address-of
operator and a class qualification. For example,
class X
{
void foo();
};
A member function pointer to X::foo would be &X::foo (and never just
foo, never X::foo, and never &foo.
Quote

BUT when I simply use If/else:

if (EnableActions)
ActionMakeDistribFiles->OnUpdate = ActionMakeDistribFilesUpdate;
else
ActionMakeDistribFiles->OnUpdate = NULL;

it compiles and works fine.
This is directly assigning to the closure, without the machinery of
the ternary operator getting in the way and confusing the compiler.
In that regard, I think __closures are not first-class objects in BCB,
but are only partially integrated for the common cases.
--
Chris (TeamB);
 

Re:ternary operator ?: used with member function pointers

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Well, in this case I'd suggest using the correct syntax and use
&Class::method :)
Except that wouldn't work for method pointers that are obtained from
an explicit object pointer, ie:
ActionMakeDistribFiles->OnUpdate =
&(SomeObj->ActionMakeDistribFilesUpdate);
How would you compile such a "&Class::method" statement from a
runtime-assigned pointer? ;-)
Gambit
 

Re:ternary operator ?: used with member function pointers

Remy Lebeau (TeamB) wrote:
Quote
Except that wouldn't work for method pointers that are obtained from
an explicit object pointer, ie:

ActionMakeDistribFiles->OnUpdate =
&(SomeObj->ActionMakeDistribFilesUpdate);

How would you compile such a "&Class::method" statement from a
runtime-assigned pointer? ;-)


That is an incorrect syntax. I'm not sure what you are trying to show...
 

Re:ternary operator ?: used with member function pointers

Quote
ActionMakeDistribFiles->OnUpdate = EnableActions ?
ActionMakeDistribFilesUpdate : NULL;
Don't you mean?
ActionMakeDistribFiles->OnUpdate == EnableActions ?
ActionMakeDistribFilesUpdate : NULL;
/Palle
 

Re:ternary operator ?: used with member function pointers

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >writes:
Quote
Remy Lebeau (TeamB) wrote:

>Except that wouldn't work for method pointers that are obtained from
>an explicit object pointer, ie:
>ActionMakeDistribFiles->OnUpdate =
>&(SomeObj->ActionMakeDistribFilesUpdate);
>How would you compile such a "&Class::method" statement from a
>runtime-assigned pointer? ;-)
>
That is an incorrect syntax. I'm not sure what you are trying to show...
It's the BCB non-standard syntax for creating __closure objects. (A
binding between a member function and an object on which to call it.)
--
Chris (TeamB);
 

Re:ternary operator ?: used with member function pointers

Chris Uzdavinis (TeamB) wrote:
Quote
It's the BCB non-standard syntax for creating __closure objects. (A
binding between a member function and an object on which to call it.)

I've never used that syntax...