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 

"Inherited", "absolute", "Nil" - in Pascal; ?????? - in C++B

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





PostPosted: Wed Sep 03, 2003 7:15 am    Post subject: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in C++B Reply with quote



Hi,
I just found a declaration in Paslal (Delphi component source) like:
What is inherited I know, but how to call an inherited method in C++Builder?
Please give me some sugestions...

------------------- begin -----------------------
procedure TCustomMPHexEditor.Loaded; // where Loaded() is declared as
Virtual function
begin
inherited;
CreateEmptyFile(UNNAMED_FILE);
end;
-------------------- end ------------------------

Second Q: how to translate a nothing pointer var = NIL from Pascal to C++.
var = 0 is usable in this case.

And one more Q: how to translate ABSOLUTE directive from Pascal to
C++Builder.
---------------------- begin ---------------------
Value : integer;
LCrdTemp: cardinal absolute Value;
----------------------end------------------------



Back to top
Ralph Kazemier
Guest





PostPosted: Wed Sep 03, 2003 8:39 am    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote




"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
What is inherited I know, but how to call an inherited method in
C++Builder?
Please give me some sugestions...

C++ does not support the inherited keyword. You must specify the scope of
the function yourself. The scope in this case is your base class;

void __fastcall TCustomMPHexEditor::Loaded()
{
baseclass::Loaded();
CreateEmptyFile(UNNAMED_FILE);
}


I usually declare a type in the class itself, called inherited;

class TCustomMPHexEditor: public TComponent
{
typedef TComponent inherited;

public:
virtual void __fastcall Loaded();
};

void __fastcall TCustomMPHexEditor::Loaded()
{
inherited::Loaded();
CreateEmptyFile(UNNAMED_FILE);
}



Quote:
Second Q: how to translate a nothing pointer var = NIL from Pascal to C++.
var = 0 is usable in this case.

Could you please be more specific? However, in general, NULL (C++) is the
equivalent for Nil (Pascal).


Quote:
And one more Q: how to translate ABSOLUTE directive from Pascal to
C++Builder.
---------------------- begin ---------------------
Value : integer;
LCrdTemp: cardinal absolute Value;
----------------------end------------------------

In the example above, the absolute directive instructs that LCrdTemp uses
the same memory space as Value does. In other words, a type-cast. In C++
this could translate to;

int Value;
Cardinal* LCrdTemp = reinterpret_cast<Cardinal*>(&Value);



Ralph




Back to top
Serge CSM
Guest





PostPosted: Wed Sep 03, 2003 9:10 am    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote



Hi Ralph,

Quote:
Second Q: how to translate a nothing pointer var = NIL from Pascal to
C++.
var = 0 is usable in this case.

Could you please be more specific? However, in general, NULL (C++) is the
equivalent for Nil (Pascal).

Ok! It's right. I used = 0 and =NULL. The results was the same in both
cases.
But the problem is how my code for example will be interpreted by C++Builder
3.

Take a look to other definition of Nil:
http://www.bridgespublishing.com/articles/issues/9707/Using_Delphi_component
s_in_C++Builder.htm
Look in chapter "More const issues" and you will se this other definition:

--------------------- cut
here --------------------------------------------------------
const DefPointer = nil; //Pascal source code
This declaration causes C++Builder to generate the following declaration in
the header:
const void * DefPointer = (void *)(0x0);
I don't know why this particular declaration is translated into a const
declaration rather than a #define. Perhaps Borland will fix this problem in
future versions of C++Builder. For the time being, you can work around it by
declaring the constant as follows:

const DefPointer = 0;
---------------------- cut here ---------------------------




Back to top
Ralph Kazemier
Guest





PostPosted: Wed Sep 03, 2003 10:18 am    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote


"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
Take a look to other definition of Nil:

[...]

The article involves the problems that may arise when C++ Builder compiles
Delphi code. In this case header file generation in particular. If you declare
a const variable in a Delphi unit;

const
DefPointer = nil;

it will create the following entry in a .hpp file;

const void * DefPointer = (void *)(0x0);

The article then states;

"Although this declaration looks bizarre, the greater sin is
that this const declaration appears in a header file."

IMO the declaration is not bizarre. It not a sin that the declaration appears
in a header file. However, what could cause problems here is that the
definition is in the header file, as the article explains;

"Let's say you have a project that has multiple source code
units. In addition, more than one of those source units
#includes the header containing this declaration. The
application will compile with no problems, but it will fail
at link time, complaining that the variable DefPointer
is declared in one unit and again in another unit."


Quote:
But the problem is how my code for example will be interpreted
by C++Builder 3.

Your original question was: "how to translate a nothing pointer var = NIL from
Pascal to C++.". So, I presume you are porting Delphi code C++ yourself. If
so, you have nothing to worry about. Simply declare the variable in a .h file
and define it in a .cpp file;

..h:
---
extern const void * DefPointer;

..cpp:
-----
const void * DefPointer = (void *)(0x0);


Doing so, DefPointer is defined only once and thus will not cause linker errors.
This is common practice and will compile correctly in BCB1,3,4,5 and 6.


Ralph




Back to top
Serge CSM
Guest





PostPosted: Wed Sep 03, 2003 10:47 am    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote

Thank's Ralph for very good explication!

I just read about Delphi to C++ conversion on borland comunity web-site.
and found the same info about header creation-translation of Pascal source
used by DCC32.EXE of C++Builder pack.

See you later... Smile
I just finish to print from PDF the book "Borland C++ 6 Developer guide"
(1180 pages!!!) and send it to binder. After that "I think!" i'll have a
less questions... and don't grab your time.

Thank's.
Serge.




Back to top
Ralph Kazemier
Guest





PostPosted: Wed Sep 03, 2003 12:28 pm    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote


"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
I just finish to print from PDF the book "Borland C++ 6 Developer guide"
(1180 pages!!!) and send it to binder.

Wow! Smile How long did that take?


Ralph



Back to top
Serge CSM
Guest





PostPosted: Wed Sep 03, 2003 1:30 pm    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote

Quote:
I just finish to print from PDF the book "Borland C++ 6 Developer guide"
(1180 pages!!!) and send it to binder.
Wow! Smile How long did that take?

Hmmm. About 40-50 min on "HP LaserJet 5000 PS". Book is allready in my
hands!
Great Book! Check email.

Ralph one more Q:
----------------------------------
function TCustomMPHexEditor.GetMouseOverSelection: boolean;
var LPntMouse: TPoint;
begin
Windows.GetCursorPos(LPntMouse);
LPntMouse := ScreenToClient(LPntMouse);
Result := CursorOverSelection(LPntMouse.x, LPntMouse.y);
end;
----------------------------------

I just translated it C++ and get an errror:
--------------------------------
bool __fastcall TCustomMPHexEditor::GetMouseOverSelection(void)
{
TPoint * LPntMouse;
::GetCursorPos(LPntMouse);

LPntMouse = ScreenToClient(LPntMouse); // E2064 Cannot initialize 'const
TPoint &' with 'TPoint *'

//E2342 Type mismatch in parameter 'Point' (wanted 'const TPoint &', got
'TPoint *')

E2034 Cannot convert 'TPoint' to 'TPoint *'

return CursorOverSelection(LPntMouse.X, LPntMouse.Y); //E2294 Structure
required on left side of . or .*
}

-----------------------------
Where is my mistake? I can undestand in what cases I must use?:
1. TPoint * LPntMouse;
2. TPoint LPntMouse;
3. LPntMouse = new TPoint(this)






Back to top
Ralph Kazemier
Guest





PostPosted: Wed Sep 03, 2003 1:58 pm    Post subject: Re: "Inherited", "absolute", "Nil" - in Pascal; ?????? - in Reply with quote


"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
I just translated it C++ and get an errror:

[...]


Quote:
TPoint * LPntMouse;
::GetCursorPos(LPntMouse);

You pass an uninitialized pointer to GetCursorPos() here. You should either
dynamically allocate memory;

TPoint * LPntMouse = new TPoint;
::GetCursorPos( LPntMouse );

...

delete LPntMouse;

or just pass the address of a TPoint;

TPoint LPntMouse;
::GetCursorPos( &LPntMouse );

The latter is probably best, since you won't have to concern yourself with
memory (de)allocation. Besides you need a TPoint futher on anyway ;)


Quote:
LPntMouse = ScreenToClient(LPntMouse); // E2064 Cannot initialize 'const
TPoint &' with 'TPoint *'

The compiler tries to create a temporary TPoint object for you, but fails since
TPoint does not have a constructor taking an TPoint* as parameter.


Quote:
//E2342 Type mismatch in parameter 'Point' (wanted 'const TPoint &', got
'TPoint *')

E2034 Cannot convert 'TPoint' to 'TPoint *'


ScreenToClient() expects a TPoint& (reference to TPoint). You pass a TPoint*.
That is the 'Type mismatch'. Furthermore, the compiler tries to implicitly cast
TPoint to TPoint*, but fails.


Quote:
return CursorOverSelection(LPntMouse.X, LPntMouse.Y); //E2294 Structure
required on left side of . or .*
}

Since you have declared LPntMouse as TPoint*, the compiler complains about the
fact you should use it as a pointer, e.g.;

LPntMouse->X

or

LPntMouse.*X


To sum it up, change the function to;

bool __fastcall TCustomMPHexEditor::GetMouseOverSelection()
{
TPoint LPntMouse;
::GetCursorPos( &LPntMouse );
LPntMouse = ScreenToClient( LPntMouse );

return CursorOverSelection(LPntMouse.X, LPntMouse.Y);
}


Ralph



Back to top
Serge CSM
Guest





PostPosted: Wed Sep 03, 2003 2:13 pm    Post subject: Re: TPoint structure Reply with quote

Hi Ralph,
Just found in the book:

Quote:
return CursorOverSelection(LPntMouse.X, LPntMouse.Y);

All is perfect only one little mistake here:

return CursorOverSelection(LPntMouse.y, LPntMouse.y)

TPoint structure is quite diferent from other structures like TGridCoord for
example. (Why bordand made this, why he used lower- and upper case leters
for coordinates XY).
There must be used lowercase leters ".x" and ".y".

All done. Thank's
Serge.



Back to top
Serge CSM
Guest





PostPosted: Thu Sep 04, 2003 6:56 am    Post subject: Re: Inherited again Reply with quote

Quote:
Hi Ralph,

I'm finishing my component but got some Q that I never found prior:
1. How to understand (translate) next code:
--------------------------------------
procedure TCustomMPHexEditor.Loaded;
begin
inherited; // <- ????
CreateEmptyFile(UNNAMED_FILE);
end;
-------------------------------------

2. And this code:
---------------------------------------------------
procedure TCustomMPHexEditor.Resize;
begin
PostMessage(Handle, WM_INTUPDATECARET, 7, 7);
inherited;
end;
---------------------------------------

What is the differense between first and second declaration?

Thank's.
Serge.
P.S. Waiting for you mail....




Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Sep 04, 2003 7:41 am    Post subject: Re: Inherited again Reply with quote

"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
1. How to understand (translate) next code:
--------------------------------------
procedure TCustomMPHexEditor.Loaded;
begin
inherited; // <- ????
CreateEmptyFile(UNNAMED_FILE);
end;

Ralph already gave you that exact answer in his first reply. Please do back
and have another look at his examples.


Gambit


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.515 / Virus Database: 313 - Release Date: 9/1/03



Back to top
Serge CSM
Guest





PostPosted: Thu Sep 04, 2003 7:59 am    Post subject: Re: Inherited again Reply with quote

Hi Gambit,

Quote:
Ralph already gave you that exact answer in his first reply. Please do
back
and have another look at his examples.

You did not undestand me...

What is the differense between calling "inherited" before and after calling
procedure.
Or: What is a diference between calling process of Example 1 and Example 2
in Pascal:

1. inherited; // <- ????
CreateEmptyFile(UNNAMED_FILE);

2. CreateEmptyFile(UNNAMED_FILE);
inherited;

This procedures are equivalent?







Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Sep 04, 2003 5:12 pm    Post subject: Re: Inherited again Reply with quote


"Serge CSM" <gunivas (AT) moldovacc (DOT) md> wrote


Quote:
What is the differense between calling "inherited" before
and after calling procedure.

There is no difference in the calls themselves. They both do the exact same
thing - invoke the base class functionality. When you choose to actually do
that is not important unless you are writing code that depends on the result
of the base class handling, or you simply do not want the base class to
handle the operation until after you have first.


Gambit


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.515 / Virus Database: 313 - Release Date: 9/1/03




Back to top
Timothy H. Buchman
Guest





PostPosted: Tue Sep 09, 2003 4:33 pm    Post subject: Re: Inherited again Reply with quote

Ralph Kazemier <ralph (AT) kazemier (DOT) demon.nl> wrote

Quote:
That is the difference Smile It depends on what the inherited
function does
really. Have a look at the following example;
FYI, it might be good practice to avoid this kind of design. Users
of the
class X might be wondering (just like you are) how to call the
inherited
function. In the following example there is no need to call the
inherited
function at all;

It doesn't reflect badly at all on Ralph's excellent discussion to
observe for Serge's benefit that there are, at the very least, cases
in the Borland VCL where it is essential to call inherited functions.
These include overridden methods of components, particularly Events
that subsequent user's of the derived component have the option of
supplying their own event handler for. As Ralph points out, the
location of the inherited:: call often determines whether the user's
code has the intended effect.

--
Timothy H. Buchman
========================================
City Center Theater, New York NY
mail address tbuchmanPLEASE(at sign)REMOVEcitycenter.org
Search .borland message archive on http://www.mers.com/searchsite.html



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) 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.