English 中文(简体)
在接触私人阶级成员时,如何使用朋友功能? [重复]
原标题:How is the friend function accessed in the case of accessing private class member? [duplicate]
This question already has answers here: Can I access private members from outside the class without using friends? (27 answers) Closed 6 hours ago. This post was edited and submitted for review 1 hour ago. This is a tricky tactic to access a private member of class. But I can t get the point of declaration of friend get inside struct A_member? #include struct A { private: int member; }; template struct Rob { friend typename Tag::type get(Tag) { return M; } }; // tag used to access A::member struct A_member { typedef int A::*type; friend type get(A_member); }; template struct Rob; int main() { A a; a.*(get(A_member())) = 42; // write 42 to it std::cout << "proof: " << a.*get(A_member()) << std::endl; } According to the cppreference: (only allowed in non-local class definitions) Defines a non-member function, and makes it a friend of this class at the same time. The explicit instantiation of struct Rob has made the newly defined get a non-member function, in the closest namespace, i.e. global namespace here. So can it be accessed directly in the main? I suppose that the get has been defined in the global namespace already by the explicit instantiation, thus I don t understand the purpose of the friend declaration of get in struct A_member. Without this line, compiling gets error: struct A_member { typedef int A::*type; // friend type get(A_member); }; : In function int main() : :25:7: error: get was not declared in this scope; did you mean std::get ? Back to the above code, it redeclare a friend get inside the struct A_member and then magically it works. What s the purpose of this friend declaration? Is there some special c++ rule that I missed?
问题回答
#include class MyClass { private: int privateData; public: MyClass(int data) : privateData(data) {} // Declare a friend function friend void accessPrivateData(const MyClass& obj); }; // Definition of the friend function void accessPrivateData(const MyClass& obj) { // Access private member of MyClass std::cout << "Private Data: " << obj.privateData << std::endl; } int main() { MyClass myObject(42); accessPrivateData(myObject); // Accesses private data via the friend function return 0; }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签