Skip to content

Instantly share code, notes, and snippets.

@congdanhqx-zz
Last active July 25, 2016 10:03
Show Gist options
  • Save congdanhqx-zz/0335e9c77b4d42202f98 to your computer and use it in GitHub Desktop.
Save congdanhqx-zz/0335e9c77b4d42202f98 to your computer and use it in GitHub Desktop.
How to access private member in C++
#include <iostream>
template <class Tag, typename Tag::type Value>
struct PrivateRobber {
friend typename Tag::type get(Tag) {
return Value;
}
};
class Item {
public:
Item(int value) : m_value(value) {
}
friend std::ostream& operator<<(std::ostream& os, const Item& item) {
return os << item.m_value;
}
private:
int m_value;
};
struct TagIntItem {
// define `type` as type `int Item::*`
typedef int Item::*type;
// friend declaration for code from line 5 to line 7
friend type get(TagIntItem);
};
template struct PrivateRobber<TagIntItem, &Item::m_value>;
int main()
{
Item item(1);
std::cout << "Item: " << item << "\n";
// call function get in line 5, return an object of type `int Item::*`
// a.k.a. `TagIntItem::type`, then dereference it with item
int &value = item.*get(TagIntItem());
std::cout << "Value: " << value << "\n";
std::cout << "Changing value to 10...\n";
value = 10;
std::cout << "Changed value to 10...\n";
// No matter how code will be reordered,
// this line will be executed after changing variable.
std::cout << "Item: " << item << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment