[英]Is there a way to declare objects within a conditional statement?
我無法根據用戶的選擇搞清楚如何正確創建對象。
在程序中,我問用戶他們想要成為他們的班級 - 騎士或巫師。 我輸入'1'或'2'代表騎士和巫師。
我做了一個switch語句,在case 1中,我聲明了一個對象Knight,對於Wizard也是如此。
我需要在switch語句之外使用這些對象,但我不能。 我試圖制作一個'默認'對象,制作'玩家播放器'; 但由於Player類具有純虛函數,我也不能這樣做。
我該如何有效地做到這一點?
這是我到目前為止:
int main()
{
std::string plyrName;
int input;
bool foo = false;
std::cout << "What is your name?\n";
std::cin >> plyrName;
std::cin.ignore(1000, '\n');
std::cout << "\nWelcome, " << plyrName << ". What class would you like to be?\n";
std::cout << "1. Knight.\n2. Wizard.\n";
std::cin >> input;
while (input != 1 && input != 2)
{
if (foo == true)
std::cout << "Please enter 1 for Knight and 2 for Wizard.\n";
if (!(std::cin >> input))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\n";
std::cout << "Only integers are allowed.\n";
}
else
std::cout << "\n";
foo = true;
}
switch (input)
{
case 1:
{
Wizard player;
break;
}
case 2:
{
Knight player;
break;
}
}
std::cout << "\nHere is your player information summary.\n";
std::cout << player.classType();
system("pause");
return 0;
}
我需要在創建之后訪問播放器對象,因為我想向用戶輸出他們選擇的類。 Knight和Wizard類都有一個輸出它的功能。
編輯:我有一個跟進問題。 在圖中,Knight&Wizard有一個靜態變量'特殊攻擊名'。 如何在main函數中訪問此變量? 使用unique_ptr的解決方案意味着指針將指向基類Player,因此不允許訪問派生類成員,例如靜態變量'special attack name'。 我的設計有缺陷嗎?
在你的情況下,因為你想要完成多態,你應該去指針和引用。 為什么? 我強烈推薦這個美麗的答案。 為什么沒有指針/引用時多態性不起作用?
那么,你應該尋找一個像Player *
這樣的原始指針嗎?
在幾乎所有場景中,你都應該永遠不會選擇原始指針,特別是當它指向動態內存時。 只是因為任何編程錯誤或異常都可能導致delete
被delete
。
因此,我強烈建議您使用C ++ 11中引入的智能指針,如unique_ptr
和shared_ptr
,它們遵循RAII模式並保證取消初始化。
以下是您的案例中unique_ptr
的使用示例。
#include <memory>
using PlayerPtr = std::unique_ptr<Player>;
using KnightPtr = std::unique_ptr<Knight>;
using WizardPtr = std::unique_ptr<Wizard>;
int main()
{
...
PlayerPtr playerPtr = nullptr;
switch (input) {
case 1: {
playerPtr = KnightPtr(new Knight);
}
break;
case 2: {
playerPtr = WizardPtr(new Wizard);
}
break;
}
// use playerPtr outside.
}
編輯:
正如HTNW正確指出的那樣 ,你必須使用std::make_unique
而不是使用new
。 但請記住,這是一個C ++ 14概念。 您必須具有編譯器支持。
如果你在switch case范圍內創建你的變量,只要你離開那個范圍引導你到UB就會被刪除,所以你把它聲明為指針所以它可以比條件語句更長,即:你之前將它聲明為基類指針你把它給它指向條件陳述里面的地方
#include<memory>
int main()
{
std::string plyrName;
int input;
bool foo = false;
//create your player ptr
std::unique_ptr<Game_Object> player;
std::cout << "What is your name?\n";
std::cin >> plyrName;
std::cin.ignore(1000, '\n');
std::cout << "\nWelcome, " << plyrName << ". What class would you like to be?\n";
std::cout << "1. Knight.\n2. Wizard.\n";
std::cin >> input;
while (input != 1 && input != 2)
{
if (foo == true)
std::cout << "Please enter 1 for Knight and 2 for Wizard.\n";
if (!(std::cin >> input))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\n";
std::cout << "Only integers are allowed.\n";
}
else
std::cout << "\n";
foo = true;
}
switch (input)
{
case 1:
{ // initialize it and it would work perfectly as you intend
player = std::make_unique<WIZARD>();
break;
}
case 2:
{ //****
player = std::make_unique<KNIGHT>();
break;
}
}
std::cout << "\nHere is your player information summary.\n";
std::cout << player->classType();
system("pause");
return 0;
}
如果我錯了,有人會糾正我,但是對象只在它們創建的范圍內有效,所以一旦你離開那個switch語句,那些對象就不再可訪問了。
您應該在switch語句之外聲明一個GAME OBJECT類對象,然后創建向導或騎士(因為這兩個類都繼承了GAME OBJECT)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.