簡體   English   中英

如何通過 C++ 中的字符串輸入來執行 class 方法

[英]How to execute a class method by string input in C++

我正在嘗試在 C++ 中開發文本冒險,用戶可以在其中輸入字符串命令(例如“拿蘋果”)。 這是我想出的一個非常天真的代碼示例:

# include <iostream>     
using namespace std;

class fruit{
    public:
        string actual_name;
        fruit(string name){
            actual_name = name;
        }
        take() {
            cout << "You take a " << actual_name << "." << endl;
        }
};

fruit returnObjectFromName(string name, fruit Fruits[]){
    for(int i = 0; i <= 1; i++){  // to be modified in future depending on Fruits[] in main()
        if (Fruits[i].actual_name == name)
            return Fruits[i];
        }
}

int main(){
    string verb;
    cout << "Enter verb: ";
    cin >> verb;
    string object;
    cout << "Enter object: ";
    cin >> object;
    fruit apple("apple");
    fruit Fruits[] = { apple }; // to be extended in future
    // returnObjectFromName(object, Fruits). ??? ()
}

如果這可能的話,我怎么可能得到與 function returnObjectFromName 類似的東西的水果方法? 我從 Python(獨立)開始開發,在那里我至少可以使用 eval(),但正如我在 C++ 中理解的那樣,這不是一個選項。 我也嘗試過使用 map,但我沒有設法讓它與方法一起使用。

謝謝大家的答案。

依靠 C++ 中的反射不是好方法,我認為沒有辦法在類中列出方法。 也許您可以使用 function 指針,但指向實例方法的指針是地獄。

我建議使用多態性和良好的設計。 如果有些物品可能會被拿走,那么使用這樣的界面:

#include <iostream>

using namespace std;

class ITakeable {
    public:
        virtual bool isTakeable() = 0;
        virtual void take() = 0;
        virtual void cannotTake() = 0;
};

class fruit : public ITakeable {
    public:
        string actual_name;
        
        fruit(string name){
            actual_name = name;
        }
        
        bool isTakeable() {
            return true;
        }
        
        void take() {
            cout << "You take a " << actual_name << "." << endl;
        }
        
        void cannotTake() {
            cout << "not needed to be implemented";
        }
};

class airplane : public ITakeable {
    public:
        string actual_name;
        
        airplane(string name){
            actual_name = name;
        }
        
        bool isTakeable() {
            return false;
        }
        
        void take() {
            cout << "not needed to be implemented";
        }
        
        void cannotTake() {
            cout << "You CANNOT take a " << actual_name << "." << endl;
        }
};


int main() {
   fruit apple("apple");
   
   if (apple.isTakeable()) {
       apple.take();
   }
   
   airplane plane("boeing");

   if (plane.isTakeable()) {
       plane.take();
   } else {
       plane.cannotTake();
   }
   
   // use of interface in general
   
   ITakeable * something = &apple;

   if (something->isTakeable()) {
       something->take();
   }
   
   something = &plane;

   if (something->isTakeable()) {
       something->take();
   } else {
       something->cannotTake();
   }

   return 0;
}

由於fruit 是用戶定義的類型,因此您必須為您的類型聲明自己的方法,或者從先前定義的方法繼承。

有很多“內置”字符串類型的方法可以執行與 python 中的 eval (...) 幾乎相同的工作。

我還注意到您的 function 不需要在 class 水果之外獨立定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM