C++和類C++類初始化
我有一個名爲「精靈」級,當該被初始化它需要一個布爾變量:
// constructor
Sprite::Sprite(bool type)
{
// set stuff
}
// two Sprites of different types
Sprite Hero(1)
Sprite Enemy(0)
Q.如何初始化的陣列100個類型爲0的精靈?
雪碧敵人[100]?
C++和類C++類初始化
我有一個名爲「精靈」級,當該被初始化它需要一個布爾變量:
// constructor
Sprite::Sprite(bool type)
{
// set stuff
}
// two Sprites of different types
Sprite Hero(1)
Sprite Enemy(0)
Q.如何初始化的陣列100個類型爲0的精靈?
雪碧敵人[100]?
您可以使用默認的構造函數,這將只是默認爲0值,就像這樣:
//default constructor
Sprite::Sprite()
{
//set to false
}
Sprite::Sprite(bool type)
{
// set to type
}
現在 雪碧敵人[100] 將工作
或者用少一點的代碼使用默認值像這樣的構造:
Sprite::Sprite(bool type=false)
{
//set to type
{
我的建議是,你使用std::vector
,然後用constructor取一個值的論點。
像
std::vector<Sprite> Enemies(100, Sprite(false));
您可能需要適當的拷貝構造函數和拷貝賦值爲Sprite
定義爲它工作的運營商。
如果你沒有(這可能是你的情況好轉或std::array
)載體,那麼你必須聲明數組作爲一個正常的數組,然後使用一個循環來初始化每個條目:
Sprite Enemies[100];
for (size_t i = 0; i < 100; ++i)
Enemies[i].set(false);
非常感謝 - 這實際上是一個Arduino項目,不知道如果Arduino有載體 - 我看到somone已經寫了自己的:http://forum.arduino.cc/index.php/topic,45626.0.html不是reall我以後通過 –
@JakeFrench如果你正在使用一個符合標準的C++編譯器,應該有向量。 –
只使用簡單的Arduino草圖 –
除非我錯了,否則不能直接使用構造函數和對象數組的參數。一種解決方案是使用std::vector
:
std::vector<Sprite> Ennemies(100, Sprite(false));
std::vector<Sprite> Ennemies(100, {false}); // C++11 style
如果你真的想C數組,你可以得到它,通過一個例子:
Sprite* pEnnemies = &Ennemies.front();
另外一個解決方案是使用新的C++ 11容器std::array
這是隻有在STL語法C風格數組:
std::array<Sprite, 100> Ennemies(Sprite(false));
如果你打算使用C++ 11風格,你也應該使用'std :: vector'構造函數的大括號。 :) –
創建的類的陣列時,它們必須由默認構造函數創建的。
你可以爲你的類型參數添加一個默認值「= false」,然後代碼就可以工作。雖然它不是很靈活,但您可能需要其他類型的數組。
另一種方法是讓你的精靈在用不同類型構造後重置。創建空白精靈數組後,在for循環中調用它們的reset(type)。
如果您確實需要在您的元素上使用非默認構造函數,請使用std :: vector。有兩種方法可以做到這一點。
std::vector<Sprite> Enemies;
Enemies.reserve(100);
for (int i = 0; i < 100; i++)
{
Enemies.push_back(Sprite(0));
}
或
std::vector<Sprite> Enemies(100, Sprite(0));
使用默認參數:
Sprite::Sprite(bool type=false) : mymember(type)
{}
,然後當你聲明:
Sprite Enemies[100];
它會調用所有的100種元素的默認構造函數。
你應該瞭解成員初始化列表,在C++寫一個構造函數的正確方法是:
Sprite(bool type=false); // declaration
...
Sprite::Sprite(bool type) : // definition
x(random(0, 82)),
y(random(0, 20)),
Ammo(25),
Alive(true),
Type(type)
{}
非常感謝所有采取一看,看完評論之後,我發現這個工程,超載構造函數:
class Sprite
{
public:
Sprite();
Sprite(bool type);
void Move();
private:
unsigned int x, y, Ammo;
bool Alive;
bool Type;
};
// constructor
Sprite::Sprite()
{
Alive = true;
Type = 0;
Ammo = 25;
x = random(0, 82);
y = random(0, 20);
}
Sprite::Sprite(bool type)
{
Alive = true;
Type = 1;
Ammo = 25;
x = 20; // get x from btn press
y = 10; // get y from btn press
}
Sprite Hero(1);雪碧敵人[100];
這不是如何去做。看我的答案重寫您的構造函數。 –
好的,總是樂於學習 –
爲了完整起見,有使用展示位置定位new
最後一個方法:
unsigned char data[sizeof(Sprite) * 100];
Sprite* ptr = reinterpret_cast<Sprite*>(&data[0]);
Sprite* end = ptr + 100;
for (; ptr != end; ++ptr)
new (ptr) Sprite(0);
這是非常有什麼其他的答案,不依賴於拷貝構造函數內部使用。
如果Sprite
有一個不平凡的析構函數,你將不得不顯式調用它使用另一個循環在data
的生命週期的末尾:
Sprite* ptr = reinterpret_cast<Sprite*>(&data[0]);
Sprite* end = ptr + 100;
for (; ptr != end; ++ptr)
ptr->~Sprite();
爲什麼downvotes?我覺得這是一個有趣的問題。 – TobiMcNamobi
我同意@TobiMcNamobi !!,讓我們把它顛倒過來,給它一些upvotes –
另一方面...這看起來像[C++:使用非默認構造函數動態分配結構的成員數組] (http://stackoverflow.com/questions/6146025/c-dynamically-allocating-a-member-array-of-structs-using-non-default-construc) – TobiMcNamobi