繁体   English   中英

C ++如何将引用从一个函数传递给另一个函数?

[英]C++ how can I pass reference from one function to another?

我有下一个功能:

void UserInterface::showMenu(vector<GraphicsCard> *allCards) {
    int currentCard = 0;
    string userInput = "";

    cout << "1: Show Previous, 2: Show Next" << endl;
    cin >> userInput;

    switch (stoi(userInput)) {
        case 1:
            if (currentCard > 0) {
                currentCard--;
            }
            UserInterface::showGraphicsCardTable(&allCards[currentCard]);
            UserInterface::showMenu(allCards);
            break;
        case 2:
            if (currentCard < allCards->size()) {
                currentCard++;
            }
            UserInterface::showGraphicsCardTable(&allCards[currentCard]);
            UserInterface::showMenu(allCards);
            break;
        default:
            break;
    }
}

我试图将对向量的特定元素的引用传递给void UserInterface::showGraphicsCardTable(GraphicsCard *card)函数。 问题是&allCards[currentCard]在这种情况下不起作用。 如何进一步传递参考?

更改此:

UserInterface::showGraphicsCardTable(&allCards[currentCard]);

对此:

UserInterface::showGraphicsCardTable(&((*allCards)[currentCard]));

顺便说一句,为什么要在传递一个指针? 而是在showMenu()传递引用! ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM