클래스 생성자에서 초기화되는 구조체가 있습니다. 구조체의 콜백을 호출 할 때 해당 구조체의 메모리 주소를 포인터로 사용하여 포인터를 초기화해야합니다. 매개 변수로 가지고있는 클래스의 인스턴스를 보낼 수 있습니다.클래스 인스턴스 메모리 주소 (C++)
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
클래스 I는 TouchZone
구조체를 만드는거야의 생성자에서 : 여기
TouchZone
을 만든
Button
클래스의 특정 인스턴스에 대한 포인터를 저장하려고합니다. 생성자의 내부에서 올바르게 수행하는 방법을 알 수 없으며 가능하면 알 수 없습니다. 내가 여기서 컴파일 한 것은 컴파일되지만 포인터와 메모리 주소에 대한 지식이 부족하여 필자가 원하지 않는 결과를 얻는 것으로 보인다.
이것은 콜백에 대한 Button
클래스의 참조를 사용하려는 예제 함수입니다.
void HomeScreen(Button& sender) {
Serial.println(sender.getSpanX()); //Prints some number like 2133411092 but varies every time. Should be 200.
Serial.println(sender.theZone.getSpanX()); //Constantly prints 85, but should also be 200.
sender.disable();
theScreen.fillScr(VGA_MAROON);
delay(300);
theScreen.fillScr(VGA_WHITE);
Serial.println(sender.getX()); //Usually prints some number like -149863422 but varies every time
sender.resize(sender.getX(), 10, sender.getSpanX(), sender.getSpanY()); //The button disappears on the screen, which it should not.
sender.enable();
}
이 질문은 저의 첫 번째 질문입니다. 너무 부 풀릴 수 있습니다. 간단히 요약하면, 클래스의 생성자 중 내부의 클래스 인스턴스 참조를 전달하는 것이 목표입니다.
글쎄 내 의견이 답을 얻지 못했습니다 ... 그리고 귀하의 구체적인 사례에 대한 사용을 좀 더 자세히 확인해야합니다. – miradham
'this' 포인터는 생성자에서 유효합니다. 객체가 아직 완전하게 구성되지 않았으므로 사용할 수 없지만 나중에 저장하고 나중에 사용할 수 있습니다. –
@BoPersson TouchZone 구조체에 포인터를 전달할 때 그렇게하지 않습니까? 단추가 실제로 구성되고 UI에 표시 될 때까지 포인터를 사용하지 않습니다. – AadamZ5