C++ Assignment Operator as the Delphi Implicit Operator

In C++ it is possible to use the operator= for assignment to the result type, the same as the implicit operator is used in Delphi.

For instance, given the variable myVar with TValue type, in Delphi, it is possible to assign different types to this variable as follows:

{code}
a := 'assigning a string';
a := 123;
a := 3.14;
{code}
Previously in C++, without the assignment operator, you had to write it like this:
{code}
a = TValue::_op_Implicit(UnicodeString(L"assigning a string"));
a = TValue::_op_Implicit(123);
a = TValue::_op_Implicit(3.14L);
{code}

Now in C++, with the assignment operator, you can write it as:
{code}
a = UnicodeString(L"assigning a string");
a = 123;
a = 3.14L;
{code}

But this does not work for FMX controls for StyleData[""] or Data as follows and i don't know why:

{code}
TValue a;
a = UnicodeString(L"image.jpg");            //Works fine
a = 123;
a = 3.14L;

Panel1->StylesData["Data"] = UnicodeString(L"image.jpg");    //Error here

Image1->Data = String(L"image.jpg");           //Error here

[bcc32 Error] Unit1.cpp(25): E2034 Cannot convert 'UnicodeString' to 'TValue'
{code}

After testing it seems like trying to assign a UnicodeString to a TValue property in a class gives the above error, while doing the same to a public TValue data member works fine. For Example:

{code}
class MyTestValue
{
public:
TValue FData;
__property TValue Data = {read=FData,write=FData};
}

//Testing

MyTestValue MyTest;
MyTest.FData = UnicodeString(L"image.jpg");      //Works fine
MyTest.Data = UnicodeString(L"image.jpg");      //Cannot convert 'UnicodeString' to 'TValue'
{code}

Isn't this feature should has been working for any property with type TValue? How come this is not working?
I Want to use this with FMX controls Data and StylesData[""].

Also note that in Rad XE8 samples they are still using the old technique in "CustomListBox" example:

{code}
	TListBoxItem *Item;
	int i;

	if (OpenDialog1->Execute()) {
		for (i = 0; i < OpenDialog1->Files->Count; i++) {
			String currentFile = OpenDialog1->Files->operator [](i);
			Item = new TListBoxItem(NULL);
			Item->Parent = ListBox1;
			Item->StyleLookup = "CustomItem";
			Item->TagString = currentFile;
			Item->Text = currentFile;
			Item->StylesData["icon"] = TValue::From<String>(currentFile);
			Item->StylesData["resolution"] = TValue::From<String>("1024x768 px"); // set size
			Item->StylesData["depth"] = TValue::From<String>("32 bit");
			Item->StylesData["visible"] = TValue::From<bool>(true); // set Checkbox value
			Item->StylesData["visible.OnChange"] = NotifyEventAsTValue(DoVisibleChange); // set OnChange value
			Item->StylesData["info.OnClick"] = NotifyEventAsTValue(DoInfoClick); // set OnClick value
		}
		Caption = IntToStr(ListBox1->Count) + " items";
	};
{code}

--
The limits of my language mean the limits of my world
0
Ahmed
7/13/2015 10:53:24 PM
embarcadero.cppbuilder.cpp 2803 articles. 0 followers. Follow

5 Replies
1109 Views

Similar Articles

[PageSpeed] 30

Ahmed wrote:

> But this does not work for FMX controls for StyleData[""] or Data
> as follows and i don't know why:
>
> Panel1->StylesData["Data"] = UnicodeString(L"image.jpg"); //Error here

It is because StylesData is a property that only accepts a TValue as input, 
and TValue does not have a constructor that accepts a UnicodeString as input, 
so the compiler cannot create a temporary TValue for you.  You have to provide 
your own TValue, eg:

{code}
TValue a;
a = UnicodeString(L"image.jpg");
Panel1->StylesData["Data"] = a;
{code}

Or:

{code}
Panel1->StylesData["Data"] = TValue::From<String>(L"image.jpg");
{code}

> Image1->Data = String(L"image.jpg");           //Error here

Same reason.  Data is a property that only accepts a TValue, so you have 
to construct the TValue yourself, the compiler is not able to do it for you.

> After testing it seems like trying to assign a UnicodeString to a
> TValue property in a class gives the above error, while doing the
> same to a public TValue data member works fine.

Correct, because of the property setter and the need for a real TValue instance 
as input.  Same as in the case of the StylesData and Data properties.

> Isn't this feature should has been working for any property with type
> TValue? How come this is not working?

It would work if Embarcadero had given TValue multiple constructors to match 
its assignment operators, so that a TValue can be constructed from different 
input types.  But, they did not, they introduced the TValue::From() method 
instead.  Since TValue does not have such constructors, the compiler's hands 
are tied, it cannot create implicit TValue instances for you, you have to 
construct them yourself.

> I Want to use this with FMX controls Data and StylesData[""].

The TValue::From() approach I show above, and Embarcadero showed in its sample, 
is your best choice.

> Also note that in Rad XE8 samples they are still using the old
> technique in "CustomListBox" example:

No, they are using the newer TValue::From() approach, not the older TValue::_op_Implicit() 
approach.

On the other hand, whoever thought to write a demo that calls TStrings::operator[] 
directly should have their hand smacked!  Use the Strings[] property instead:

{code}
String currentFile = OpenDialog1->Files->Strings[i];
{code}

Or deference the TStrings object pointer so the compiler can invoke TStrings::operator[] 
the "right" way:

{code}
String currentFile = (*(OpenDialog1->Files))[i];
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
7/14/2015 1:26:42 AM
> On the other hand, whoever thought to write a demo that calls TStrings::operator[] 
> directly should have their hand smacked!  Use the Strings[] property instead:
> 
> {code}
> String currentFile = OpenDialog1->Files->Strings[i];
> {code}
> 
> Or deference the TStrings object pointer so the compiler can invoke TStrings::operator[] 
> the "right" way:
> 
> {code}
> String currentFile = (*(OpenDialog1->Files))[i];
> {code}
> 
> -- 
> Remy Lebeau (TeamB)

What is the problem with operator [](i) ?
0
Utf
7/14/2015 8:46:33 AM
Milan wrote:

> What is the problem with operator [](i) ?

They are not meant to be invoked directly as methods, but rather as syntax 
elements that the compiler calls as needed.

-- 
Remy Lebeau (TeamB)
0
Remy
7/14/2015 5:05:51 PM
Ahmed wrote:

> But this does not work for FMX controls for StyleData[""] or Data
> as follows and i don't know why:
>
> Panel1->StylesData["Data"] = UnicodeString(L"image.jpg"); //Error here

It is because StylesData is a property that only accepts a TValue as input, 
and TValue does not have a constructor that accepts a UnicodeString as input, 
so the compiler cannot create a temporary TValue for you.  You have to provide 
your own TValue, eg:

{code}
TValue a;
a = UnicodeString(L"image.jpg");
Panel1->StylesData["Data"] = a;
{code}

Or:

{code}
Panel1->StylesData["Data"] = TValue::From<String>(L"image.jpg");
{code}

> Image1->Data = String(L"image.jpg");           //Error here

Same reason.  Data is a property that only accepts a TValue, so you have 
to construct the TValue yourself, the compiler is not able to do it for you.

> After testing it seems like trying to assign a UnicodeString to a
> TValue property in a class gives the above error, while doing the
> same to a public TValue data member works fine.

Correct, because of the property setter and the need for a real TValue instance 
as input.  Same as in the case of the StylesData and Data properties.

> Isn't this feature should has been working for any property with type
> TValue? How come this is not working?

It would work if Embarcadero had given TValue multiple constructors to match 
its assignment operators, so that a TValue can be constructed from different 
input types.  But, they did not, they introduced the TValue::From() method 
instead.  Since TValue does not have such constructors, the compiler's hands 
are tied, it cannot create implicit TValue instances for you, you have to 
construct them yourself.

> I Want to use this with FMX controls Data and StylesData[""].

The TValue::From() approach I show above, and Embarcadero showed in its sample, 
is your best choice.

> Also note that in Rad XE8 samples they are still using the old
> technique in "CustomListBox" example:

No, they are using the newer TValue::From() approach, not the older TValue::_op_Implicit() 
approach.

On the other hand, whoever thought to write a demo that calls TStrings::operator[] 
directly should have their hand smacked!  Use the Strings[] property instead:

{code}
String currentFile = OpenDialog1->Files->Strings[i];
{code}

Or deference the TStrings object pointer so the compiler can invoke TStrings::operator[] 
the "right" way:

{code}
String currentFile = (*(OpenDialog1->Files))[i];
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
7/14/2015 10:54:43 PM
Milan wrote:

> What is the problem with operator [](i) ?

They are not meant to be invoked directly as methods, but rather as syntax 
elements that the compiler calls as needed.

-- 
Remy Lebeau (TeamB)
0
Remy
7/14/2015 10:56:15 PM
Reply:

Similar Artilces:

Delphi implicit operator in C++
Hi! The code generated in hpp is wrong for implicit operators declared in pascal records. The code compiles, but returns wrong results. Is there a way to tell the compiler to leave out these functions? Thanks! Atmapuri ...

C# operator -----> VB.NET operator
Hi, I´m looking at some C#-code but can´t find an equal expression in VB.NET x >>= y Someone that know? br, PatrikB'## -- second place is the first looser -- Hi, take a look at this whitepaper at MS support: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308470 It discuss about differences in VB.NET and C# related to syntax etc.Thanks,Teemu KeiskiFinland, EU Couldn´t find anything about the subject .... but, I got an answer at the Swedish Deveoper Forum http://www.webforum.nu But, I post the answer I got there in here ... if other pe...

Can assignment meta-operator be combined with feed operators?
S06 says that we need to say "eager" if (@in === @out). So: @data ==> eager map { $^x + 1 } ==> @data. Is it possible to modify a feed operator using the assignment meta-operator described in S02 and, if so, is the "eager" implict? @data ==>= map { $_ + 1 }; On Fri, Jan 11, 2008 at 01:56:44PM -0800, Dave Whipp wrote: > S06 says that we need to say "eager" if (@in === @out). So: > > @data ==> eager map { $^x + 1 } ==> @data. > > > Is it possible to modify a feed operator using the assignment meta-opera...

Operating on Operation Attributes
It's a given that when creating a condition group for a rule, we often base them upon the status of the operation attributes. A simple example being: Code: -------------------- <if-op-attr name="Telephone Number" op="changing"/> -------------------- I've made a note that if I ever want to determine if an attribute is being removed, that I have to combine two conditions: Code: -------------------- <if-op-attr name="Telephone Number" op="changing"/> <if-op-attr name="Telephone Number" op...

Statement, operators, operation.
Hi I just got a paging procedure from the internet and it’s working just fine, but I would like to return the numbers of pages too. So I used the Count() function to retrieve all records on the table and divided by the page size, but even I doing with float number, this is returning a integer number. I just started with SQL Server, does some knows what is wrong with the following code? or knows a better way to do it.   Declare     @Records int,             @Pages float,       &nb...

Implicit class operator and record assignment
Hi all, I am trying to make deep copy of the record structure using Implicit class operator, but compiler throws me an error E2521 Operator 'Implicit' must take one 'TMyRecord' type in parameter or result type {code} type TMyRecord = record public Value: TBytes; class operator Implicit(const aValue: TMyRecord): TMyRecord; end; class operator TMyRecord.Implicit(const aValue: TMyRecord): TMyRecord; begin SetLength(Result.Value, Length(aValue.Value)); Move(aValue.Value[0], Result.Value[0], Length(aValue.Value)); end; var a, b: TMyRecord...

Error 409: The assignment operator operation could not take a text data type as an argument
How can I make it work when I need to pull out a field which is text type using a stored procedure? Please help!!!Thanks I am getting the following error Error 409: The assignment operator operation could not take a text data type as an argument ===========my sp================================= CREATE PROCEDURE [dbo].[sp_SelectABC] (@a varchar(50) output, @b text output ) AS set nocount on select @a=name, @b= description from ABC GO No, you can't do that. You're going to have to replace it with select name, description from ABC http://www.rocksth...

hyper/vector operation operator
Hello everyone! First of all - I do not closely follow perl6/parrot development. I read "= this=20 week on perl6" on www.perl.com but that's it - so if I'm completly off th= e=20 track, let me know. Regarding the discussions about the hyper operator (eg adding elements of= 2=20 arrays into another array) I've had the following idea: use "=3D>" - in perl5 there is an operator "=3D>" which is used in associative array= =20 assignment. In perl6 this means "pairs" IIRC, which could get interpreted= as=20 "add p...

Assignment Operator
Hi All, I'm studying Perl since one week on "Learning Perl" written by L. Wall = and in the paragraph "Assignment Operators" i don't understand why this: ($temp =3D $global) +=3D $constant; is equivalent of: $tmp =3D $global + $constant; Instead, before i read it, i thought it was equivalent of: =09 $temp =3D $global; $temp =3D $temp + $constant; Regards= 2009/11/26 Marco Pacini <info@marcopacini.org>: > Hi All, > > I'm studying Perl since one week on "Learning Perl" written by L. Wall an= d in the paragr...

assign operator
--0-1281669910-1260525753=:34541 Content-Type: text/plain; charset=us-ascii Hi All, Can somebody please tell me what is the difference between "=" and ":=" sign in case of perl?? Regards Irfan. --0-1281669910-1260525753=:34541-- Irfan Sayed: > Hi All, > > Can somebody please tell me what is the difference between "=" and ":=" sign in case of perl?? > I never saw ":=" in any perl code. Also I checked perlop and didn't find that a symbol. http://perldoc.perl.org/perlop.html -- Jeff Pan...

Examples for tr -C to implement tr//C operator
Does anyone have non-ASCII (Unicode) testcases for POSIX tr -C (complement, collation order, see http://pubs.opengroup.org/onlinepubs/007904875/utilities/tr.html)? A coworker likes to implement tr///C (opposed to tr///c) like POSIX tr -C but I like to have Unicode testcases for that work. Irek ...

LIKE Operator in C#
Hello !I am using following function in ASP. How can I use in ASP.NET in c#?Dim chch = "1"If ch Like "[A-Za-z0-9]" Then   Response.Write "Good"End IfThank you,Hemchandहेमंत चंदुरकर.पुणे.भारत. using System.Text.RegularExpressions;string s;s = "1";Regex r = new Regex( "[A-Za-z0-9]" );if ( r.Match( s ).Success )   Response.Write( "good" ); If you want to do a range search like that, check out the System.Text.RegularExpressions namespace....

~ Operator
Why is this operator listed in redirect ~/anypage.aspx Can anyone please explain Thanks Jijo The tilde (~) represents the Application's root folder. This allows you to deploy a site to a different folder without having to touch all of your path references. Ex: WebApp URL = http://myserver/ => ~ = / WebApp URL = http://myserver/TomatoStore => ~ = /TomatoStore...

C# || operator question
I am trying to use || as an 'or' operator in an if statement. Any idea why it doesn't work? string strTest = "A"; if (strTest != "A" || strTest !="B") { Response.Write("It must be C"); } else { Response.Write("It is " + strTest); } || has higher priority than != Try if((strTest != "A") || (strTest != "B"))Best regards, LesioS Thanks LesioS. Unfortunately that didn't do it. Changing the || to && does the trick. I just had to think through it logically. No. ...

Web resources about - C++ Assignment Operator as the Delphi Implicit Operator - embarcadero.cppbuilder.cpp

Assignment - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

HootSuite Rolls Out HootSuite Assignments Google Chrome Extension
Social-media-management platform HootSuite announced the release of HootSuite Assignments , a browser extension for Google Chrome that enables ...

FNC Assignment Desk (@foxnewsdesk)
Log in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here FNC Assignment Desk @ foxnewsdesk ...

TeacherKit - Class Organizer, Teacher Planner, Gradebook, Assignment List, Attendance and Student 's ...
Get TeacherKit - Class Organizer, Teacher Planner, Gradebook, Assignment List, Attendance and Student 's Grade Tracker on the App Store. See ...

Assignments - Flickr - Photo Sharing!
106/365 As you can see, I have a lot to work on this weekend. All of my assignments are due on Monday, so time to get crackin'. Oh, and when ...

On Assignment - YouTube
On Assignment, Voice of America's 30-minute weekly TV magazine, introduces you to the correspondents and producers who bring you news from around ...

Animal of the Day: Mittens the Hermaphroditic Cat to Get Gender Assignment Surgery
Meanwhile in Canada… A cat born with both male and female genitalia in Newfoundland is about to go under the knife. Mittens was found in the ...

School assignments go high-tech
How much do you help your kids with their school projects? How much do you help your kids with their school projects? Remember when the pinnacle ...

Students enlist MyMaster website to write essays, assignments
Students from almost every university in NSW are paying up to $1000 to have their essays written by a Sydney-based company, undermining the academic ...

Snow Sky confirms spring assignment as England stayer enters quarantine - DailyTelegraph Search Search ...
ONE of the leading fancies for the Melbourne Cup, top English stayer Snow Sky has been confirmed as heading to our shores after entering quarantine ...

Resources last updated: 11/30/2015 6:58:34 AM