原标题: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);
};
问题回答
#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;
}
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...