为什么当非const是私有的时,不调用公共const方法? [英] Why is a public const method not called when the non-const one is private?
问题描述
请考虑此代码:
struct A
{
void foo()const
{
std :: cout<< const< std :: endl;
}
private:
void foo()
{
std :: cout< non-const< std :: endl;
}
};
int main()
{
A a;
a.foo();
}
编译器错误是:
错误:'void A :: foo()'is private`。
当我删除私人的,它只是工作。为什么当非const的方法是私有的时,不调用public const方法?
换句话说,为什么重载解析在访问控制之前?这很奇怪。你认为它是一致的吗?
当你调用 a.foo();
,编译器通过重载分辨率找到要使用的最佳函数。当它建立重载集合时,它找到
void foo()const
pre>
和
void foo()
现在,由于
a
不是const
,非const版本是最好的匹配,所以编译器选择void foo()
。然后访问限制就位,你会得到一个编译错误,因为void foo()
是私有的。
记住,在重载分辨率下,它不是'找到最好的可用功能'。它是找到最好的功能,并尝试使用它。如果由于访问限制或被删除而导致无法访问,则会出现编译错误。
换句话说,在访问控制之前?
好吧,让我们看看:
struct Base
{
void foo(){std :: cout< Base\\\
; }
};
struct Derived:Base
{
void foo(){std :: cout< Derived\\\
; }
};
struct Foo
{
void foo(Base * b){b-> foo(); }
private:
void foo(Derived * d){d-> foo(); }
};
int main()
{
Derived d;
Foo f;
f.foo(& d)
}
现在让我们说, > void foo(Derived * d) private。如果访问控制首先是这个程序将编译和运行,并且将打印
Base
。这可能很难在大代码库中跟踪。因为访问控制来自重载解决后,我得到一个很好的编译器错误告诉我的功能,我想要它调用不能被调用,我可以找到的错误很容易。Consider this code:
struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); }
The compiler error is:
error: 'void A::foo()' is private`.
But when I delete the private one it just works. Why is the public const method not called when the non-const one is private?
In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code works and then I add a method, and my working code does not compile at all.
解决方案When you call
a.foo();
, the compiler goes through overload resolution to find the best function to use. When it builds the overload set it findsvoid foo() const
and
void foo()
Now, since
a
is notconst
, the non-const version is the best match, so the compiler picksvoid foo()
. Then the access restrictions are put in place and you get a compiler error, sincevoid foo()
is private.Remember, in overload resolution it is not 'find the best usable function'. It is 'find the best function and try to use it'. If it can't because of access restrictions or being deleted, then you get a compiler error.
In other words why does overload resolution comes before access control?
Well, let's look at:
struct Base { void foo() { std::cout << "Base\n"; } }; struct Derived : Base { void foo() { std::cout << "Derived\n"; } }; struct Foo { void foo(Base * b) { b->foo(); } private: void foo(Derived * d) { d->foo(); } }; int main() { Derived d; Foo f; f.foo(&d); }
Now let's say that I did not actually mean to make
void foo(Derived * d)
private. If access control came first then this program would compile and run andBase
would be printed. This could be very hard to track down in a large code base. Since access control comes after overload resolution I get a nice compiler error telling me the function I want it to call cannot be called, and I can find the bug a lot easier.这篇关于为什么当非const是私有的时,不调用公共const方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!