Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can we access address of private member(data)

Reply
Thread Tools

can we access address of private member(data)

 
 
eric
Guest
Posts: n/a
 
      08-13-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: litb is teh famous (private access specifier is private no more) jacob navia C++ 1 04-03-2012 11:14 AM
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
Can different instantiations of the same template can access others' private member? PengYu.UT@gmail.com C++ 1 10-19-2005 05:52 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM



Advertisments
 



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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57