C++ Protected / Public overloads

后端 未结 4 1892
無奈伤痛
無奈伤痛 2021-01-19 05:28

I have a class like this :

class Foo
{
public:
    Foo()
    {
        for(int i = 0; i < 10; ++i)
            v.push_back(i);
    };
    const vector<         


        
4条回答
  •  被撕碎了的回忆
    2021-01-19 06:21

    Since foo is not const compiler is trying to use the non-const method. As a workaround you can do the following:

        Foo foo;
        const Foo& cFoo = foo;
        for(int i = 0; i < (int) cFoo.V().size(); ++i)
        cout << cFoo.V().at(i) << endl;
    

提交回复
热议问题