| View previous topic :: View next topic |
| Author |
Message |
Ralph Kazemier Guest
|
Posted: Wed Sep 10, 2003 11:06 am Post subject: Re: " is " operator in Pascal. |
|
|
"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote
| Quote: | procedure TMyColors.Assign(Source: TPersistent); //overiden
begin
if (Source is TMPHColors) then
Background := TMPHColors(Source).Background; <------ waht that mean?
Source is a TPersistent reference. This line casts Source to TMPHColors, so |
that you can access members of that class.
Ralph
|
|
| Back to top |
|
 |
Ralph Kazemier Guest
|
Posted: Wed Sep 10, 2003 11:09 am Post subject: Re: " is " operator in Pascal. |
|
|
"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote
| Quote: | Please tell me how to translate operator " is " from Pascal to C++.
|
Use dynamic_cast for that in C++.
void __fastcall TMyColors::Assign(TPersistent* Source)
{
TMyColors* S = dynamic_cast<TMyColors*>(Source);
if( S )
{
} else {
// your baseclass below
baseclass::Assign(Source);
}
}
Ralph
|
|
| Back to top |
|
 |
Peter Agricola Guest
|
Posted: Wed Sep 10, 2003 11:20 am Post subject: Re: " is " operator in Pascal. |
|
|
The 'is' operator checks if a component belongs to a particular class. C++
equivalent is the typeid operator. In C++ it would translate to:
#include <typeinfo>
if ( typeid(*Source) == typeid(TMPHColors)) or
if ( typeid(*Source).name() == "TMPHColors")
You can use the dynamic_cast for this purpose too, the equivalent of the
pascal 'as' operator. 'is' only checks, 'as' also tries to perform a cast.
"Serge CSM" <gunivas (AT) moldovacc (DOT) md> schreef in bericht
news:3f5ef7ec (AT) newsgroups (DOT) borland.com...
| Quote: | Sorry forget to ask some dimmed commands:
procedure TMyColors.Assign(Source: TPersistent); //overiden
begin
if (Source is TMPHColors) then
Background := TMPHColors(Source).Background; <------ waht that mean?
end;
|
void TMyColors::Assign(TPersistent* Source)
{
TMPHColors* colors = dynamic_cast
if(colors)
Background = colors->Background;
}
Peter
|
|
| Back to top |
|
 |
Peter Agricola Guest
|
Posted: Wed Sep 10, 2003 11:45 am Post subject: Re: " is " operator in Pascal. |
|
|
| Quote: |
procedure TMyColors.Assign(Source: TPersistent); //overiden
begin
if (Source is TMPHColors) then
Background := TMPHColors(Source).Background; <------ waht that mean?
end;
|
Sorry, forgot to answer that.
Because Source is of type TPersistent you can't access the Background
property although Source is pointing to a TMPHColors. You have to cast
Source first. This is another way of casting in pascal.
It is equivalent to (Source as TMPHColors).Background, although the latter
is safer in the same way
as dynamic_cast
C++. But you did already the test so you don't need that safety and runtime
overhead.
Peter
|
|
| Back to top |
|
 |
Serge CSM Guest
|
Posted: Wed Sep 10, 2003 11:47 am Post subject: Re: " is " operator in Pascal. |
|
|
Hi Ralph,
I 'm geting an error that tell next:
--------------- Pascal source ------------------------
procedure TMPHColors.Assign(Source: TPersistent);
begin
if Source is TMPHColors then
begin
Background := TMPHColors(Source).Background;
....
Grid := TMPHColors(Source).Grid;
end;
end;
------------------------------------------------------
------- Translated C++Builder
source ---------------------------------------
void __fastcall TMPHColors::Assign(Classes::TPersistent* Source) //overriden
{
TMPHColors * S = dynamic_cast<TMPHColors*>(Source);
if( S )
{
S->Background = TMPHColors(Source).Background; <<<<<<<< Errors
...
S->Grid := TMPHColors(Source).Grid; <<<< The same errors
}
}
----------------------------------------------------------------------------
----
Errors:
[C++ Error] MPHexEditor.h(5302): E2285 Could not find a match for
'TMPHColors::TMPHColors(TPersistent *)'
[C++ Error] MPHexEditor.h(5302): E2459 VCL style classes must be constructed
using operator new
And one more Q:
The TMPHColors constructor in C++ must be implemented before or below
procedure TMPHColors.Assign(...).
Or it's not important? In Pascal it's implemented below
TMPHColors.Assign(...).
Thank's. Serge.
|
|
| Back to top |
|
 |
Ralph Kazemier Guest
|
Posted: Wed Sep 10, 2003 12:00 pm Post subject: Re: " is " operator in Pascal. |
|
|
"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote
| Quote: | TMPHColors * S = dynamic_cast<TMPHColors*>(Source);
if( S )
{
S->Background = TMPHColors(Source).Background; <<<<<<<< Errors
|
You are mixing Delphi code with C++ here. You already casted the Source, so
you should use S. Besides that, you assign something to a member of S
(which is the Source for the assignment). Change it into something like;
TMPHColors * S = dynamic_cast
if( S )
{
FBackground = S->Background;
...
| Quote: | ...
S->Grid := TMPHColors(Source).Grid; <<<< The same errors
}
|
Same here. ( PS. ':=' is probably a typo? )
FGrid = S->Grid;
| Quote: | And one more Q:
The TMPHColors constructor in C++ must be implemented before or below
procedure TMPHColors.Assign(...).
Or it's not important? In Pascal it's implemented below
TMPHColors.Assign(...).
|
That doesn't matter.
Once a class' defined (e.g. in a header file) you can use it. After all the
source files of your project have been compiled, the linker will try to
'link' the implementation for you. It could even be in another source file
or object file (.obj), as long as that object file is linked to the target
(exe, bpl, dll, etc.)
Ralph
|
|
| Back to top |
|
 |
|