Board index » cppbuilder » Why Delphi can access Private member but C++ CAN'T!!?!?!?!?

Why Delphi can access Private member but C++ CAN'T!!?!?!?!?


2003-09-10 09:50:25 PM
cppbuilder103
Hi All,
I have a defined class TCustomMPHexEditor with private member Color
I'm getting an error (see below ERROR) when trying to assign a value to
Color proprety. WHY THAT ERROR don't apper in Pascal?
---------------------------------------------------------------------
class PACKAGE TCustomMPHexEditor : public Grids::TCustomGrid
{
private:
__property Color = {default=-2147483643};
....
}
class TMPHColors : public Classes::TPersistent
{
private:
Controls::TControl* FParent;
void __fastcall SetBackground(const Graphics::TColor Value);
....
}
void __fastcall TMPHColors::SetBackground(const Graphics::TColor Value)
{
TCustomMPHexEditor * S = dynamic_cast<TCustomMPHexEditor*>(FParent);
if (FParent != NULL)
S->Color = Value; <<--------------------ERROR:
}
-------------------------------------------------------------------
ERROR: [C++ Error] MPHexEditor.h(5347): E2247 'TCustomMPHexEditor::Color' is
not accessible
----------The same in
Pascal----------------------------------------------------
TCustomMPHexEditor = class(TCustomGrid)
private
property Color;
.....
end;
TMPHColors = class(TPersistent)
private
FParent: TControl;
procedure SetBackground(const Value: TColor);
...
end;
procedure TMPHColors.SetBackground(const Value: TColor);
begin
if Assigned(FParent) then
begin
TCustomMPHexEditor(FParent).Color := Value;
FParent.Invalidate;
end;
end;
------------------------------------------------------
Serge.
 
 

Re:Why Delphi can access Private member but C++ CAN'T!!?!?!?!?

That is because classes in the same unit are implicitly friends of each
other in Pascal.
if you want the same behaviour in C++ add to your TCustomMPHexEditor
declaration this line:
class PACKAGE TCustomMPHexEditor : public Grids::TCustomGrid
{
private:
friend class TMPHColors;
Peter
 

Re:Why Delphi can access Private member but C++ CAN'T!!?!?!?!?

"Serge CSM" wrote:>
Quote
void __fastcall TMPHColors::SetBackground(const Graphics::TColor Value)
{
TCustomMPHexEditor * S = dynamic_cast<TCustomMPHexEditor*>(FParent);
if (FParent != NULL)
S->Color = Value; <<--------------------ERROR:
}
This is dangerous code. If FParent is assigned a value other then a
TCustomMPHexEditor your code fails if that class doesn't have a color
property. You should test:
if(S != 0) or shorter if(S).
Peter
 

{smallsort}

Re:Why Delphi can access Private member but C++ CAN'T!!?!?!?!?

On Wed, 10 Sep 2003 16:50:25 +0300, Serge CSM wrote:
Quote
if (FParent != NULL)
S->Color = Value; <<--------------------ERROR:
Presumably you meant:
if (S) {
S->Color = Value;
}
--
liz