How to get private member pointer?

class A
{
private:
std::string a1_str_;
std::string a2_str_;
};

Although the following code is illeagal, but I want to use it to illustrate what I want.

A a;
std::string* str1_ptr = &a.a1_str_;
std::string* str2_ptr = &a.a2_str_;
Hi,

You can't do this, it's private - any attempt to do so would be illegal: one can't (reliably) subvert the security.

Instead write a public function that uses the string to do whatever processing, and return the result.

Good Luck !!
thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <iomanip>

class A
{
    // ...

    private:
        std::string a1_str_ = "first" ;
        std::string a2_str_ = "second" ;

    friend class A_test_driver ;
};

class A_test_driver
{
    public: static void debug_dump( const A& a )
    {
        std::clog << "A @ " << std::addressof(a) << "\n{\n\t"
                  << std::quoted(a.a1_str_) << " @ " << std::addressof(a.a1_str_)
                  << "\n\t" << std::quoted(a.a2_str_) << " @ " << std::addressof(a.a2_str_)
                  << "\n}\n" ;
    }
};

int main()
{
    A a ;
    A_test_driver::debug_dump(a) ;
}

http://coliru.stacked-crooked.com/a/ed6519a6a2b1c378
Topic archived. No new replies allowed.