How to implement the Delphi protected member access trick in C++ builder?

802 views Asked by At

I need access to TControlItem.InternalSetLocation which is protected. I Delphi you would do

type
  THackControlItem = class(TControlItem);

How do you do this in C++ Builder?

2

There are 2 answers

0
serge On

As in Delphi, you need to inherit the class but also override and make public the protected function. However, I wouldn't recommend to use it in production code.

class THackControlItem : public TControlItem
{
public:
    void __fastcall InternalSetLocation(int AColumn, int ARow, bool APushed, bool MoveExisting)
    {
        TControlItem::InternalSetLocation(AColumn, ARow, APushed, MoveExisting);
    }
};

In the program

TControlItem* ci = ...;
static_cast<THackControlItem*>(ci)->InternalSetLocation(...);
8
Spektre On

This is a nice trick I think Remy Lebeau showed me but can not find the QA anymore...

//---------------------------------------------------------------------------
#ifndef _TDirectMemoryStream
#define _TDirectMemoryStream
class TDirectMemoryStream:TMemoryStream // just for accessing protected SetPointer
    {
public:
    void SetMemory(BYTE *ptr,DWORD siz) { SetPointer(ptr,siz); Position=0; };
    };
#endif
//---------------------------------------------------------------------------

You simply create new class that is descendant of the class you want to access. Now just add get/set functions for the protected members ...

Now usage:

TMemoryStream *mem=new TMemoryStream(); // original class instance you want to access

// overtype to our new class and access/use you get/set ...
((TDirectMemoryStream*)(mem))->SetMemory(hdr->lpData,hdr->dwBytesUsed);

delete mem; // release if not needed anymore

I am using it btw to feed a memory stream with custom memory data hdr coming from vfw camera so I can properly decode it using TJPEGImage class instead of writing the data into file and loading it back each frame ...

Here another example:

class A
    {
protected:
    int x;
public:
    int getx(){ return x; }
    };

class hack_A:A
    {
public:
    void setx(int _x){ x=_x; }
    };

void test()
    {
    A a;
    hack_A *ha=(hack_A*)&a;
    ha->setx(10);
    a.getx(); // print the x somwhere
    }

However this will not work for private members ... In such case its doable too but requires access to A source code:

class A
    {
protected:
    int x;
private:
    int y;
public:
    int getx(){ return x; }
    int gety(){ return y; }
    friend class hack_A;        // but this one requires access to A soourcecode
    };

class hack_A:A
    {
public:
    void setx(int _x){ x=_x; }
    void sety(int _y){ y=_y; }
    };

void test()
    {
    A a;
    hack_A *ha=(hack_A*)&a;
    ha->setx(10);
    ha->sety(20);
    a.getx(); // print the x somwhere
    a.gety(); // print the x somwhere
    }