[英]simplifying overloaded class function
我在類gTexture中聲明了兩個函數:
public:
gTexture():mActiveTexture(0){...}
virtual void DrawTexture();
virtual void DrawTexture(unsigned short int TextureNumber);
int mActiveTexture;
哪里
void gTexture::DrawTexture()
{
gTexture::DrawTexture(mActiveTexture);
}
我想在一個功能中有這樣的東西
virtual void DrawTexture(unsigned short int TextureNumber=mActiveTexture);
這不會編譯,因為mActiveTexture是對非靜態數據成員的無效使用。 有什么方法可以只具有一個函數,這將使我的派生對象更容易處理? 謝謝。
我可以看到兩種方式。 兩者都可以解決,但可能很有用。
一種是使無參數函數成為非虛擬函數; 它將始終總是使用mActiveTexture
調用第二個。
另一種方法是使用領域知識(特別是0
不是有效的OpenGL紋理名稱這一事實)並執行以下操作:
virtual void DrawTexture(unsigned short int TextureNumber = 0) {
if (TextureNumber == 0) TextureNumber = mActiveTexture;
// ... rest of the code
}
您可能有一個無效的默認參數值(例如-1)。 然后,您的函數可以檢查該值是否為默認值,如果是默認值,請使用mActiveTexture,否則它將使用該參數。
不,您不能使用成員作為默認值。 默認值在編譯時使用。
但是,您可以為默認值使用特殊值,例如-1,然后在DrawTexture中檢查輸入值是否為-1,如果輸入為-1,則將其設置為mActiveTexture。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.