小编Fel*_*nto的帖子

C++:非类型函数指针

如何在不保存返回类型的情况下保存指向函数的指针?
例如:

int GetInt() { return 5; }
string GetStr() { return "abc"; }

FunctionPointerClass GetAny;

int main()
{
    GetAny = &GetInt;
    auto var = GetAny();

    GetAny = &GetStr;
    auto var2 = GetAny();

    cout << var << '\n' << var2;
}
Run Code Online (Sandbox Code Playgroud)

编辑

一个简单的方法是使用variant<>(感谢@sehe),如下所示:

#include <boost/variant.hpp>
#include <string>
#include <iostream>
#include <functional>

int         GetInt() { return 5;     }
std::string GetStr() { return "abc"; }

int main()
{
    std::function<boost::variant<int, std::string>()> Get;
    Get = &GetInt;
    std::cout << Get() << '\n';

    Get …
Run Code Online (Sandbox Code Playgroud)

c++ pointers function c++11

3
推荐指数
1
解决办法
93
查看次数

C ++返回一行

我正在编写一个多行系统,如下所示:

string readLines(string x)
{
    string temp = "a";
    vector<string> lines(0);
    string result;

    while (1)
    {
        cout << x;
        getline(cin, temp)

        if(temp != "")
        {
            result = result + "\n" + temp;
            lines.push_back(temp);
        }
        else
            break;
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

工作正常,但是我希望能够编辑上一行,例如,我正在输入如下内容:

Helo,
World
Run Code Online (Sandbox Code Playgroud)

我想重新helo修复我的错字。我怎样才能做到这一点?

c++ c++11

2
推荐指数
1
解决办法
6174
查看次数

标签 统计

c++ ×2

c++11 ×2

function ×1

pointers ×1