是 C++保证调用成员函数的某个常量?

发布于 2024-10-31 04:59:27 字数 648 浏览 1 评论 0原文

如果我有以下内容:

class A {
    int foo() const {
       j++;
       return i;
    }

    int& foo() {
       return i;
    }

    int i;
    mutable int j;
};

那么显然类似

A a;
a.foo() = 5;

调用非常量版本。但是需要满足哪些条件才能确保调用的是 const 或非常量版本,举几个例子......

int i = a.foo(); //I would expect to call the const. Is it guaranteed?

const int j = a.foo(); //Ditto

const int& l = a.foo(); //Ditto

int& k = a.foo(); //I would expect the non-const
foobar(k); //foobar is "void foobar(int)"
return; //but the compiler could potentially decide the const version is fine.

If I have the following:

class A {
    int foo() const {
       j++;
       return i;
    }

    int& foo() {
       return i;
    }

    int i;
    mutable int j;
};

then obviously something like

A a;
a.foo() = 5;

calls the non-const version. But what conditions need to be met to make sure that a call is to the const or non-const version for a few examples...

int i = a.foo(); //I would expect to call the const. Is it guaranteed?

const int j = a.foo(); //Ditto

const int& l = a.foo(); //Ditto

int& k = a.foo(); //I would expect the non-const
foobar(k); //foobar is "void foobar(int)"
return; //but the compiler could potentially decide the const version is fine.

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

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

发布评论

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

评论(4

风轻花落早 2024-11-07 04:59:27

当对象本身是 const 时,将调用 const 函数。

int i = static_cast<const A>(a).foo(); //calls const function

另请参阅此代码以更好地理解:(必须阅读注释

void f(const A &a) //a is const inside the function
{
    int i = a.foo(); // calls const function
}
void h(A &a) //a is non-const inside the function
{
    int i = a.foo(); // calls non-const function
}

A a;
f(a);
h(a); //pass the same object!

请参阅在线演示:http:// /ideone.com/96flE

const function gets called when the object itself is const.

int i = static_cast<const A>(a).foo(); //calls const function

Also see this code for better understanding: (must read the comments)

void f(const A &a) //a is const inside the function
{
    int i = a.foo(); // calls const function
}
void h(A &a) //a is non-const inside the function
{
    int i = a.foo(); // calls non-const function
}

A a;
f(a);
h(a); //pass the same object!

See the online demo : http://ideone.com/96flE

清风不识月 2024-11-07 04:59:27

a 的常量决定了哪个函数 - 您对返回值所做的操作不属于重载决策的一部分。您的所有示例都将调用非常量版本。

The constness of a decides which function - what you do with the return value is not a part of overload resolution. All your samples would call the non-const version.

转角预定愛 2024-11-07 04:59:27

在确定采用哪个重载时,从不考虑返回值。

另外,当 a 声明为 时

A a;

,非常量版本优先。

如果 a 被声明为

const A a;

那么 const 版本只能被调用。

Return values are never considered when determining which overload to take.

Also, when a is declared as

A a;

then the non const version takes precedence.

If a is declared as

const A a;

then the const version can be called only.

一场春暖 2024-11-07 04:59:27

您的成员函数调用是否解析为 const 成员函数,取决于“this”指针的常量性,即隐式传递给被调用成员函数的点或箭头运算符的 LHS 上的对象。

解析为非 const:

A a;
a.foo();

解析为 const:

void bar(const A& a)
{
   a.foo();
}

Whether your member function call resolves to a const member function or not, depends on the constness of the "this" pointer i.e. the object on the LHS of dot or arrow operator that is implicitly passed to the called member function.

Resolves to non const:

A a;
a.foo();

Resolves to const:

void bar(const A& a)
{
   a.foo();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文