C++受保护/公共重载

发布于 2024-11-09 23:02:08 字数 785 浏览 5 评论 0原文

我有一个这样的类:

class Foo
{
public:
    Foo()
    {
        for(int i = 0; i < 10; ++i)
            v.push_back(i);
    };
    const vector<double>& V() const {return v;};
protected:
    vector<double>& V() {return v;};
private:
    vector<double> v;
};

然后是一段这样的代码:

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

但是,后者会引发编译错误,说 V() 调用是受保护的方法,而我只是试图从中读取,不修改它。

我尝试了以下方法(但没有成功)。

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

非常感谢您的帮助。

=====

谢谢大家的解释和解决方案!非常感谢!

I have a class like this :

class Foo
{
public:
    Foo()
    {
        for(int i = 0; i < 10; ++i)
            v.push_back(i);
    };
    const vector<double>& V() const {return v;};
protected:
    vector<double>& V() {return v;};
private:
    vector<double> v;
};

And then a piece of code like this :

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

However, the latter raises a compilation error saying the V() call is a protected method while i am just trying to read from it, not modify it.

I have tried the following (but without success).

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

Many thanks for your help.

=====

Thank you all for the explanations and solutions ! It's greatly appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画尸师 2024-11-16 23:02:08

返回值不可能重载。当对象是非 const 时,将使用非 const 方法。可以通过以下方式引导编译器:

const vector<double>& test = const_cast<Foo const&>(foo).V();

或者可能更好的解决方案是让常量方法具有不同的名称(例如:ConstV)。或者您可以只添加这个新方法并将当前方法保留在那里。 C++0x 标准中使用了这种方法。例如,常量方法 cbegin()cend() 已添加到标准容器中。

It is not possible to overload on return value. Non-const method will be used when the object is non-const. It is possible to guide the compiler by:

const vector<double>& test = const_cast<Foo const&>(foo).V();

Or possibly nicer solution is to have the constant method have different name (eg.: ConstV). Or you can just add this new method and leave the current method there. This approach is used in C++0x standard. For example constant methods cbegin() and cend() have been added to standard containers.

可爱咩 2024-11-16 23:02:08

重载决策不考虑成员可访问性,因此选择理想的重载候选者,然后检查成员可访问性以查看调用是否合法。

现实的解决方法是:

Foo foo;
Foo const& foo_alias = foo;
for (std::size_t i = 0; i != foo_alias.V().size(); ++i)
    cout << foo_alias.V().at(i) << endl;

Overload resolution does not take member accessibility into account, so an ideal overload candidate is chosen and then member accessibility is checked to see if the call is legal.

The realistic workaround is:

Foo foo;
Foo const& foo_alias = foo;
for (std::size_t i = 0; i != foo_alias.V().size(); ++i)
    cout << foo_alias.V().at(i) << endl;
蓝天 2024-11-16 23:02:08

由于 foo 不是 const 编译器正在尝试使用非常量方法。作为解决方法,您可以执行以下操作:

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

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;
山田美奈子 2024-11-16 23:02:08

您已经接近解决方案了。如果 Foo 也是 const,编译器将选择 const 函数。

Foo foo;
const Foo& cfoo = foo; 

for(int i = 0; i < (int) cfoo.V().size(); ++i)
    cout << cfoo.V().at(i) << endl; 

You are close to the solution. The compiler will select the const function if the Foo is also const.

Foo foo;
const Foo& cfoo = foo; 

for(int i = 0; i < (int) cfoo.V().size(); ++i)
    cout << cfoo.V().at(i) << endl; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文