簡體   English   中英

不明白這個C ++方法是如何工作的,並希望做類似的事情

[英]Don't understand how this C++ method works, and want to do something similar

我正在嘗試為我一直在研究的大型程序添加另一個函數。 這是一款3D游戲,很多游戲都是在我到達之前建成的。 如果我想添加一些內容,我通常會尋找其他已完成類似操作的地方,並根據這些內容進行更改。 在這種情況下,我試圖學習的方法是非常復雜的,我真的不知道發生了什么(因此不知道我需要改變什么,使它做我想要它做的事情做)。

這是常規方法:

    class Action_GoToZone : public Action {
    public:
        Action_GoToZone() {}
        void eval(const Dialog& dialog, State& state) const {
            ZoneParser::getSingleton().load("../media/zones/" + mZoneFilename, false);
            GameState::getSingleton()._changeState("GameMode");
        }

        static Action* Create(const Script2::Parser::List& list) {
            Action_GoToZone* action = new Action_GoToZone();

            if(list.size() != 1)
                throw Translator::TranslateException("GoToZone Action takes exactly one parameter");

            const Script2::Parser::ListElement& e1 = list.front();
            if(!e1.mIsIdentifier)
                throw Translator::TranslateException("GoToZone Action only takes identifiers");

            action->mZoneFilename = String(e1.mIdentifier.mString);
            action->mReturnFilename = ZoneParser::getSingleton().getLastFilename();

            return action;
        }

    private:
        String mZoneFilename;
        String mReturnFilename;
    };

我想讓我的方法做的就是在不同的類中調用一個函數。 這是我嘗試的:

class Action_SetJob : public Action {
    public:
        Action_SetJob() {}
        void eval(const Dialog& dialog, State& state) const {
            GameModeState::changeJob(1); //This is the class/function I want it to call.
        }

        static Action* Create(const Script2::Parser::List& list) {
            Action_SetJob* action = new Action_SetJob();

            if(list.size() != 1)
                throw Translator::TranslateException("SetJob Action takes exactly one parameter");

            const Script2::Parser::ListElement& e1 = list.front();
            if(!e1.mIsIdentifier)
                throw Translator::TranslateException("SetJob Action only takes identifiers");

            action->GameModeState::changeJob(1);

            return action;
    private:
        int changeJob;
        }   
    };

我真的不知道什么動作 - >是...我嘗試了取出動作 - action->GameModeState::changeJob(1); 並且所有內容都低於那個但是會引發錯誤。

這可能不是解決問題的足夠信息,但如果可以,我會對有關該方法的任何解釋感到滿意。

如果我理解你正在嘗試做什么,那么這應該完成它:

class Action_SetJob : public Action {
public:
    Action_SetJob() {}
    void eval(const Dialog& dialog, State& state) const {
        GameModeState::changeJob(newJob);
    }

    static Action* Create(const Script2::Parser::List& list) {
        Action_SetJob* action = new Action_SetJob();

        if(list.size() != 1)
            throw Translator::TranslateException("SetJob Action takes exactly one parameter");

        const Script2::Parser::ListElement& e1 = list.front();
        if(!e1.mIsInteger)
            throw Translator::TranslateException("SetJob Action only takes integers");

        action->newJob = e1.mInteger.mInt;

        return action;
    }   
private:
    int newJob;
};

顯然,由於C ++是上下文敏感的(它實際上是一種不可判斷的語言),我不知道你的類有哪些其他成員,所以我猜到了可能的情況。

您想要解析一個整數,您可以將該整數傳遞給您在評估操作時嘗試調用的函數。

這假設您的changeJob方法是靜態的。 如果它實際上不是靜態的,你將不得不以某種方式找出目標對象; 例如,您可以通過向腳本函數添加其他參數來實現。

如果您想了解更多詳情,我們需要更多信息!

->是一個指針引用。 看起來你的對象是指針(來自對象類型之后的* )。 是指針操作還是其他問題? 從你寫的內容來看,除了你提到的action->之外,我並不完全確定提供什么幫助 - action->

暫無
暫無

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

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