[英]How to prevent instantiation of a C++ template class method when a specific condition is met?
[英]Delete an instantiation of a class inside of the class itself once a condition is met
我正在 Arduino 的 16x2 LCD 顯示屏上制作無限跑酷游戲。
敵人在屏幕上向左爬行,當他們到達盡頭時,我想刪除敵人 class 的實例,但我不知道如何刪除。 一旦 x position 變為零,我希望它在更新 function 中自行刪除。
不管怎樣,這是我的代碼:
class:
class Enemy {
private:
int xPos, yPos;
public:
Enemy::Enemy(int xPos, int yPos) {
this->xPos = xPos;
this->yPos = yPos;
}
draw() {
lcd.setCursor(xPos, yPos);
lcd.print("E");
}
move() {
//erase enemy at last position
lcd.setCursor(xPos, yPos);
lcd.print(" ");
//move enemy
xPos--;
if(xPos == 0){
delete(this);
}
}
};
這是 rest:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12, buttonPin = 2;
LiquidCrystal lcd = LiquidCrystal(rs, en, d4, d5, d6, d7);
unsigned int playerYPos = 1, buttonPrevious;
const long playerUpdateInterval = 32, enemyMoveInterval = 500;
unsigned long lastUpdatedPlayerMillis = 0, lastMovedEnemiesMillis;
class Enemy {
private:
int xPos, yPos;
public:
Enemy::Enemy(int xPos, int yPos) {
this->xPos = xPos;
this->yPos = yPos;
}
draw() {
lcd.setCursor(xPos, yPos);
lcd.print("E");
}
move() {
//erase enemy at last position
lcd.setCursor(xPos, yPos);
lcd.print(" ");
//move enemy
xPos--;
if(xPos == 0){
delete(this);
}
}
};
Enemy e(15, 1);
void drawPlayer() {
lcd.setCursor(1, playerYPos);
lcd.print("P");
}
void updatePlayer() {
uint8_t button = digitalRead(buttonPin);
//check if button is pressed
if (button == LOW && buttonPrevious == HIGH) {
//erase player at last position
lcd.setCursor(1, playerYPos);
lcd.print(" ");
//move player
playerYPos = 1 - playerYPos;
}
buttonPrevious = digitalRead(buttonPin);
}
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//initialize the pushbutton pin
pinMode(buttonPin, INPUT_PULLUP);
buttonPrevious = digitalRead(buttonPin);
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
//update player 30 times a second
if (currentMillis - lastUpdatedPlayerMillis >= playerUpdateInterval) {
//reset counter since last updated
lastUpdatedPlayerMillis = currentMillis;
updatePlayer();
};
//move enemies ??? times a second
if(currentMillis - lastMovedEnemiesMillis >= enemyMoveInterval) {
lastMovedEnemiesMillis = currentMillis;
e.move();
}
drawPlayer();
e.draw();
}
我試過使用delete()
function 並在谷歌上按照“如何刪除 class 或 object 自身內部的實例”的內容查找內容。
目前,它表現得很好,直到它的x
position 達到zero
,然后它拒絕刪除自己,盡管xPos
值為負,敵人在屏幕上出現故障並向左走,但在屏幕中間半隨機地重新生成展示。
從其方法之一調用 class 的析構函數不是一個好主意。 delete()
將調用 class 解除分配器,旨在銷毀使用new
表達式創建的 class 實例。 通常,您不知道這個 class 實例是如何創建的; 它可能已經分配在堆棧上,在這種情況下,結果是不可預測的。
在您的情況下,實例化了 class 的全局實例,根本不能使用delete
釋放它。
對您的代碼的一些建議:
setup()
生成你的 class 實例並使用你的指針跟蹤它,並在需要時使用loop()
刪除實例(並可能重新生成新實例)。Enemy
class 中,維護一個可以通過方法從loop()
消耗的狀態,這樣你就可以確定 class 實例何時准備好被刪除。std::unique_ptr
。 這應該減輕對原始new
/ delete
指令的需求。希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.