我有一个在类构造函数中初始化的结构,我需要结构有一个指针,该指针具有初始化它的类的内存地址,这样当我调用struct的回调时,它可以发送实例的它有一个参数。
以下是一些代码:
struct TouchZone {
int Layer;
bool Active = false;
private:
int Width;
int Height;
int X;
int Y;
Button *linkedButton;
void(*Callback)(Button &sender);
public:
TouchZone()
{
}
TouchZone(int width, int height, int x, int y, void((*callback)(Button &sender)), int lyr = 100)
{
Width = width;
Height = height;
X = x;
Y = y;
Active = true;
Callback = callback;
Layer = lyr;
}
Button* getLinkedButton() {
return linkedButton;
}
void resize(int width, int height, int x, int y, int lyr = -1) {
Width = width;
Height = height;
X = x;
Y = y;
if (lyr != -1)
{
Layer = lyr;
}
}
void UseCallback() {
Callback(*linkedButton);
}
int getX() {
return X;
}
int getSpanX() {
return X + Width;
}
int getY() {
return Y;
}
int getSpanY() {
return Y + Height;
}
bool CheckBounds(int tX, int tY) {
if (((tX >= this->X) && (tX <= this->getSpanX())) && ((tY >= this->Y) && (tY <= this->getSpanY()))) {
return true;
}
else
{
return false;
}
}
void SetLinkedButton(Button *butt) {
linkedButton = butt;
}
};
class Button {
public:
int Color;
int layer;
Outline outline;
RectType rectType;
int getSpanX();
int getSpanY();
int getX();
int getY();
bool isEnabled();
bool PressChecking = false;
void enable();
void disable();
void draw();
void resize(int ix, int iy, int isx, int isy);
Button();
Button(int ix, int iy, int isx, int isy, int color, void(*callback)(Button& sender), RectType bt, Outline ot, bool pressCheck);
static void FindButtonPressed(int x, int y);
TouchZone theZone;
private:
String Text;
static LinkedList<TouchZone> YaBoisTouches;
int X, Y, SizeX, SizeY;
};
int Button::getSpanX() {
return (this->X + this->SizeX);
}
int Button::getSpanY() {
return (this->Y + this->SizeY);
}
int Button::getX()
{
return this->X;
}
int Button::getY()
{
return this->Y;
}
Button::Button() {
}
Button::Button(int ix, int iy, int isx, int isy, int color, void(*callback)(Button &sender), RectType bt, Outline ot, bool pressCheck){
this->X = ix; this->Y = iy; this->SizeX = isx; this->SizeY = isy; this->outline = ot; this->rectType = bt; this->Color = color; this->PressChecking = pressCheck;
this->draw();
theZone = TouchZone(this->SizeX, this->SizeY, this->X, this->Y, callback);
theZone.SetLinkedButton(this); //This is where I try to pass the specific instance of this class to the struct.
Button::YaBoisTouches.add(theZone);
}
void Button::resize(int ix, int iy, int isx, int isy) {
this->X = ix; this->Y = iy; this->SizeX = isx; this->SizeY = isy;
theZone.resize(this->SizeX, this->SizeY, this->X, this->Y);
this->draw();
}
void Button::FindButtonPressed(int x, int y) {
TouchZone FoundZone;
int HighestLayer = 0;
for (int i = 0; i < Button::YaBoisTouches.size(); i++)
{
if (Button::YaBoisTouches.get(i).CheckBounds(x, y)) {
if (Button::YaBoisTouches.get(i).Layer > HighestLayer) {
HighestLayer = YaBoisTouches.get(i).Layer;
FoundZone = YaBoisTouches.get(i);
}
}
}
if (FoundZone.Active) {
if (FoundZone.getLinkedButton()->PressChecking) {
if (!pressed) { FoundZone.UseCallback(); }
}
else {
FoundZone.UseCallback();
}
}
}
在Button
类的构造函数中,我正在创建一个TouchZone
结构。在结构中,我试图存储指向创建Button
的{{1}}类的特定实例的指针。我无法弄清楚如何在构造函数中正确地执行此操作,也不知道它是否可行。我在这里编写的内容,但是我对指针和内存地址缺乏知识似乎给了我不想要的结果。
这是我打算如何使用TouchZone
类对回调的引用的示例函数。
Button
这是我的第一个问题,我认为这可能太过臃肿了。回顾一下,我的目标是将类实例引用传递给类构造函数中的结构。
答案 0 :(得分:0)
this
指针。
只有在构造函数调用完成后才会创建指针。在构造函数调用完成之前,不能使用类指针。 - miradham
答案 1 :(得分:0)
将指向Button
的指针传递给theZone
的构造函数,或者将setLinkedButton
类添加到TouchZone
,并在Button
的正文中调用它{1}}构造函数。