[英]Why can't I have certain private member functions?
Given a class, why can we implement certain private member functions, but not other private member functions?给定一个类,为什么我们可以实现某些私有成员函数,而不能实现其他私有成员函数? For instance, see below:例如,见下文:
header file:
class A
{
....
struct B
{
....
}
int f();
B* g();
}
cpp file:
int A::f(){...} // will compile
B* A::g(){...} // will not compile!
The error is "Unknown type name 'B'", but my actual code has some more details.错误是“未知类型名称‘B’”,但我的实际代码有更多细节。
B* A::g(){...}
B
is not visible outside A
. B
在A
之外不可见。
A::B* A::g(){...}
This has nothing to do with private members.这与私人成员无关。 Since B
is declared inside A
, its not in global scope, hence you need to specify what B
you are referring to.由于B
在A
内部声明,它不在全局范围内,因此您需要指定您所指的B
。
Alternatively to the other answer you can use trailing return types替代其他答案,您可以使用尾随返回类型
auto A::g() -> B* {...}
Before A::g()
you have to use A::B
.在A::g()
之前,您必须使用A::B
。 After A::g()
you can simply use B
.在A::g()
您可以简单地使用B
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.