can we access address of private member(data)

E

eric

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top