can we access address of private member(data)

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.
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
#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
----------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
The answer to your original question (thread title) is : nope!

If vc71 is letting you get away with it, it's a fault in that compiler.

I tried your code with vc9 (aka 2008) and I got the same sort of errors as you did with gcc.

If you can, you should upgrade your copy of vc!?

Andy

1>------ Build started: Project: example, Configuration: Debug Win32 ------
1>Compiling...
1>example.cpp
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3835) : error C2248: 'MyClass::ival_' : cannot access private member declared in class 'MyClass'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3829) : see declaration of 'MyClass::ival_'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3818) : see declaration of 'MyClass'
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3836) : error C2248: 'MyClass::sval_' : cannot access private member declared in class 'MyClass'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3828) : see declaration of 'MyClass::sval_'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3818) : see declaration of 'MyClass'
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3842) : error C2248: 'MyClass::ival_' : cannot access private member declared in class 'MyClass'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3829) : see declaration of 'MyClass::ival_'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3818) : see declaration of 'MyClass'
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3862) : error C2064: term does not evaluate to a function taking 1 arguments
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3864) : error C2248: 'MyClass::ival_' : cannot access private member declared in class 'MyClass'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3829) : see declaration of 'MyClass::ival_'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3818) : see declaration of 'MyClass'
1>w:\source\explore\cplusplus_vc9\example\example.cpp(3865) : error C2248: 'MyClass::sval_' : cannot access private member declared in class 'MyClass'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3828) : see declaration of 'MyClass::sval_'
1>        w:\source\explore\cplusplus_vc9\example\example.cpp(3818) : see declaration of 'MyClass'
1>Build log was saved at "file://w:\Source\Explore\cplusplus_vc9\example\Debug\BuildLog.htm"
1>example - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Topic archived. No new replies allowed.