Once again, attributes for MC++ properties

E

Edward Diener

In MC++ properties are syntactically created through member functions
declared with the __property keyword and using set_ and get_ naming
conventions. Given that a property is specified in this way, how does one
set an attribute for a property ? Does one have to precede both the set_XXX
and get_XXX __property declaration with the same attribute declaration, or
is preceding one or the other good enough to tell the MC++ compiler that the
given attribute needs to be set for the property as a whole ?
 
T

Tomas Restrepo \(MVP\)

Hi Edward,
In MC++ properties are syntactically created through member functions
declared with the __property keyword and using set_ and get_ naming
conventions. Given that a property is specified in this way, how does one
set an attribute for a property ? Does one have to precede both the set_XXX
and get_XXX __property declaration with the same attribute declaration, or
is preceding one or the other good enough to tell the MC++ compiler that the
given attribute needs to be set for the property as a whole ?

Putting it on either one should work. Actually, if you put it in both, the
resulting property metadata might end up associated with the same attribute
twice (easy to verify by using ILDASM).

Personally, I think I prefer putting it on the get_XXX...
 
T

Tomas Restrepo \(MVP\)

Hi again,
Putting it on either one should work. Actually, if you put it in both, the
resulting property metadata might end up associated with the same attribute
twice (easy to verify by using ILDASM).

Just to make this more complete: By default, an attribute applied to a
__property method will be applied to the property as a whole [1]. To get the
attribute to apply to the method, you have to precede it with "method:" like
this:

[ method: ComVisible(true) ]
__property int get_Val() { return 4; }


[1] I presume the compiler could possibly apply it by default to the method
if it's an attribute that does not apply to properties, but I doubt the
compiler actually checks this... haven't tried it, though...
 
E

Edward Diener

Tomas said:
Hi again,
Putting it on either one should work. Actually, if you put it in
both, the resulting property metadata might end up associated with
the same attribute twice (easy to verify by using ILDASM).

Just to make this more complete: By default, an attribute applied to a
__property method will be applied to the property as a whole [1]. To
get the attribute to apply to the method, you have to precede it with
"method:" like this:

[ method: ComVisible(true) ]
__property int get_Val() { return 4; }


[1] I presume the compiler could possibly apply it by default to the
method if it's an attribute that does not apply to properties, but I
doubt the compiler actually checks this... haven't tried it, though...

Thanks, that is the information I need. It does seem that this information,
about setting attributes on properties in C++, should be somewhere in the
MSDN docs for MC++, but I didn't find it anywhere there. Even more amazing,
it's not in Grimes' book <g> . I guess the documenters just assumed that one
would figure out that setting an attribute before a property get_ or set_
would set the attribute on the property as a whole.
 
T

Tomas Restrepo \(MVP\)

Hi Edward,
Thanks, that is the information I need. It does seem that this information,
about setting attributes on properties in C++, should be somewhere in the
MSDN docs for MC++, but I didn't find it anywhere there. Even more amazing,
it's not in Grimes' book <g> . I guess the documenters just assumed that one
would figure out that setting an attribute before a property get_ or set_
would set the attribute on the property as a whole.

I agree the MC++ docs do left out some things, which is rather annoying.
Fortunately, for the new CLI binding for C++ coming with whidbey (aka the
new MC++ dialect), things should be much better since we now have a much
more extensive, much more formal language specification (and the language
itself feels far more natural).

FWIW, I've added this little bit of information to my small MC++ faq
(http://www.winterdom.com/mcppfaq)... perhaps someone else might find it
useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top