BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to overcome this compiler-error (__property-problem)?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Dec 07, 2006 9:10 am    Post subject: How to overcome this compiler-error (__property-problem)? Reply with quote



The following question is more a general one (on the example of
TMediaPlayer):

Trying to create a class TMyMediaPlayer which is a layer around more
than one type olf media-player, including TMediaPlayer in the first
step I defined a property

__property TRect DisplayRect={read=GetDisplayRect,
write=SetDisplayRect};

which should access directly to TMediaPlayer's DisplayRect, if
TMediaPlayer is choosen as media-player.

I defined TMyMediaPlayer::GetDisplayRect as

TRect & __fastcall GetDisplayRect(void) {
switch(DisplayType) {
case DisplayType_None:
break;
case DisplayType_MediaPlayer:
return(MediaPlayer->DisplayRect); // <- COMPILER-ERROR
case DisplayType_...
assert(!"not implemented yet");
default:
assert(false);
}
}

where MediaPlayer and DisplayType are members of TMyMediaPlayer.

In the marked line above I get a compiler error:

E2363 Try to return a reference to a local variable <temp>
(translation of 'Versuch, eine Referenz an die lokale Variable
'<temp>' zurückzugeben')

Probably this is, because TMediaFile::DisplayRect is no real
TRect-object, but just behaves as such one (at least for reading and
writing accesses).


But how can I implement my property function?

The following should be possible:
TMyMediaPlayer *my_media_player;
my_media_player->DisplayType=DisplayType_MediaPlayer;
...
my_media_player->DisplayRect->Left=100;

The last line should really set
my_media_player->MediaPlayer->Left to 100.


Thanks a lot,

Michael
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Dec 07, 2006 9:10 am    Post subject: Re: How to overcome this compiler-error (__property-problem) Reply with quote



<MR> wrote in message news:okafn2havt49as4ng43s5etbg89jsfijog (AT) 4ax (DOT) com...

Quote:
TRect & __fastcall GetDisplayRect(void) {

Do not return the TRect by reference. You have to return it by value
instead, ie:

TRect __fastcall GetDisplayRect(void) {

Quote:
E2363 Try to return a reference to a local variable <temp
(translation of 'Versuch, eine Referenz an die lokale Variable
'<temp>' zurückzugeben')

The error is accurate. The TMediaPlayer::DisplayRect property returns a
TRect by value, not by reference. Since you are returning a TRect as a
reference, the TMediaPlayer's DisplayRect has to be stored into a temporary
variable in order to satisfy the reference, as references cannot be unbound.
But you can't return a reference to a local variable, thus the compiler
error.

Quote:
But how can I implement my property function?

Change the return value to not return the TRect by reference.

Quote:
my_media_player->DisplayRect->Left=100;

You can't do that. Asside from the syntax error (you would have to use '.'
instead of '->'), the property returns the TRect by value instead of by
reference, so any changes you make to the TRect won't be reflected in the
component. You will have to read the property into a temporary TRect,
change the Left value, and then assign the TRect back to the property, ie:

TRect tmp = my_media_player->DisplayRect;
tmp.Left =100;
my_media_player->DisplayRect = tmp;


Gambit
Back to top
Guest






PostPosted: Sun Dec 10, 2006 10:19 pm    Post subject: Re: How to overcome this compiler-error (__property-problem) Reply with quote



"Remy Lebeau \(TeamB\)" <no.spam (AT) no (DOT) spam.com> schrieb:

Quote:
my_media_player->DisplayRect->Left=100;

You can't do that. Asside from the syntax error (you would have to use '.'
instead of '->'), the property returns the TRect by value instead of by
reference, so any changes you make to the TRect won't be reflected in the
component. You will have to read the property into a temporary TRect,
change the Left value, and then assign the TRect back to the property, ie:

TRect tmp = my_media_player->DisplayRect;
tmp.Left =100;
my_media_player->DisplayRect = tmp;


My fault: when writing this I was quite sure I could write directly to
media_player->DisplayRect.Left. Because this is not possible I can't
expect to have the chance to write to
my_media_player->DisplayRect.Left.

Thanks,

Michael
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.