can we access address of private member(data)

Discussion in 'C++' started by eric, Aug 13, 2011.

  1. eric

    eric Guest

    Dear c/g++ programers:

    I am using g++ 4.5.2 to test a program, claimed can be compiled/run
    well in
    msvc++7.1 But my compiler reponse many mistake, especially it access
    the
    address of private member(data), my g++ disallow it.

    Code:
    
    #include <iostream>
    #include <string>
    
    class MyClass {
    
    public:
    MyClass() : ival_(0), sval_("foo") {}
    ~MyClass() {}
    
    void incr() {++ival_;}
    void decr() {ival_--;}
    
    private:
    std::string sval_;
    int ival_;
    };
    
    int main() {
    MyClass obj;
    
    int         MyClass::* mpi = &MyClass::ival_;  // Data member
    std::string MyClass::* mps = &MyClass::sval_;  // pointers
    
    void (MyClass::*mpf)(); // A pointer to a member function that
    // takes no params and returns void
    void (*pf)();           // A normal function pointer
    
    int* pi = &obj.ival_;   // int pointer referring to int member--no
    // problem.
    
    mpf = &MyClass::incr;   // A pointer to a member function.  You
    can't
    // write this value to a stream.  Look at it
    // in your debugger to see what its
    // representation looks like.
    
    // pf = &MyClass::incr;
    // Error: &MyClass::incr is not an instance
    // of an function
    
    std::cout << "mpi = " << mpi << '\n';
    std::cout << "mps = " << mps << '\n';
    std::cout << "pi = " << pi << '\n';
    std::cout << "*pi = " << *pi << '\n';
    
    obj.*mpi = 5;
    obj.*mps = "bar"
    
    (obj.*mpf)();  // now obj.ival_ is 6
    
    std::cout << "obj.ival_ = " << obj.ival_ << '\n';
    std::cout << "obj.sval_ = " << obj.sval_ << '\n';
    }
    
    
    ---------------------------------------------
    the result of compile
    ----------------
    Code:
    
    g++ Example15-2.cpp
    Example15-2.cpp: In function ‘int main()’:
    Example15-2.cpp:15:8: error: ‘int MyClass::ival_’ is private
    Example15-2.cpp:21:42: error: within this context
    Example15-2.cpp:15:8: error: ‘int MyClass::ival_’ is private
    Example15-2.cpp:21:42: error: within this context
    Example15-2.cpp:14:16: error: ‘std::string MyClass::sval_’ is private
    Example15-2.cpp:22:42: error: within this context
    Example15-2.cpp:14:16: error: ‘std::string MyClass::sval_’ is private
    Example15-2.cpp:22:42: error: within this context
    Example15-2.cpp:15:8: error: ‘int MyClass::ival_’ is private
    Example15-2.cpp:28:18: error: within this context
    Example15-2.cpp:48:12: error: ‘"bar"’ cannot be used as a function
    Example15-2.cpp:15:8: error: ‘int MyClass::ival_’ is private
    Example15-2.cpp:50:38: error: within this context
    Example15-2.cpp:14:16: error: ‘std::string MyClass::sval_’ is private
    Example15-2.cpp:51:38: error: within this context
    
    
    ------------------------------------------------------------------------
    this is example code 15-2 from book, c++ cookbook. You can download it
    from
    http://examples.oreilly.com/9780596007614/
    Need expert's help to modify this example program so it can compile/
    run on my
    system. And thanks a lot in advance, Eric
     
    eric, Aug 13, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy
    Replies:
    5
    Views:
    926
    JustSomeGuy
    Dec 5, 2004
  2. Richard Webb

    Large private data member access

    Richard Webb, Apr 17, 2005, in forum: C++
    Replies:
    3
    Views:
    357
    Richard Webb
    Apr 19, 2005
  3. DaveLessnau
    Replies:
    3
    Views:
    450
    Howard
    May 16, 2005
  4. Replies:
    1
    Views:
    287
    Victor Bazarov
    Oct 19, 2005
  5. Neil
    Replies:
    2
    Views:
    324
Loading...

Share This Page