我需要访问受保护的TControlItem.InternalSetLocation方法。在Delphi中,您可以这样做:
在C++ Builder中如何实现这个?
type
THackControlItem = class(TControlItem);
在C++ Builder中如何实现这个?
type
THackControlItem = class(TControlItem);
与Delphi类似,您需要继承该类并覆盖和公开受保护的函数。然而,我不建议在生产代码中使用它。
class THackControlItem : public TControlItem
{
public:
void __fastcall InternalSetLocation(int AColumn, int ARow, bool APushed, bool MoveExisting)
{
TControlItem::InternalSetLocation(AColumn, ARow, APushed, MoveExisting);
}
};
TControlItem* ci = ...;
static_cast<THackControlItem*>(ci)->InternalSetLocation(...);
我认为Remy Lebeau向我展示了一个很好的技巧,但是找不到相关的问答了...
//---------------------------------------------------------------------------
#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
//---------------------------------------------------------------------------
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
hdr
从vfw相机传送到内存流中,以便我可以使用TJPEGImage
类正确解码它,而不是将数据写入文件并在每个帧中重新加载它...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
}
A
源代码。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
}
SetMemory
一样... - Spektre