Yes I know that private
modifier created in order to prohibit access to class data
(是的,我知道创建了private
修饰符是为了禁止访问类数据)
but isn't friend
intended to allow special acces to them?
(但是,难道friend
不打算允许他们特殊访问吗?)
Compiler:
(编译器:)
main.cpp: In member function 'void C::blah(B&)':
(main.cpp:在成员函数'void C :: blah(B&)'中:)
main.cpp:48:26: error: 'int B::a' is private within this context
(main.cpp:48:26:错误:'int B :: a'在此上下文中是私有的)
std::cout << obj.a << std::endl;
(std :: cout << obj.a << std :: endl;)
Everything below is implemented the way as it is in many tutorials.
(下面的所有内容都是按照许多教程中的方式实现的。)
May be it's just silly mistake I made and blind to spot.
(可能只是我犯的愚蠢错误而盲目发现。)
Class C;
class B {
private:
int a = 2;
public:
friend void blah(B& obj);
};
class C {
public:
void blah(B& obj) {
std::cout << obj.a << std::endl; //*
}
};
*Member B::a is inaccessible
(*成员B :: a无法访问)
ask by Vladislav translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…