This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Language bug in static initializers?


I think the following should work, but I get an access
error with 2.91.66.

Static initialization expressions have special access to
private elements of a class, but I suspect that the
addresses of the member function addresses were overlooked.
That or I've screwed up the syntax. Thanks for any
feedback.

//: C11:PointerToMemberFunction2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
#include <iostream>
using namespace std;

class Widget {
  void f(int) const {cout << "Widget::f()\n";}
  void g(int) const {cout << "Widget::g()\n";}
  void h(int) const {cout << "Widget::h()\n";}
  void i(int) const {cout << "Widget::i()\n";}
  static const int cnt = 4;
  static void (Widget::*fptr[])(int) const;
public:
  void select(int i, int j) {
    if(i < 0 || i >= cnt) return;
    (this->*fptr[i])(j);
  }
  int count() { return cnt; }
};

// Initialization of static should have access
// to private addresses:
void (Widget::*fptr[])(int) const = {
    &Widget::f, // Full spec required
    &Widget::g,
    &Widget::h,
    &Widget::i,
};

int main() {
  Widget w;
  for(int i = 0; i < w.count(); i++)
    w.select(i, 47);
} ///:~



===========================================
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java" &
"Thinking in C++ 2e"
Please subscribe to my free newsletter -- just send any
email to:
join-eckel-oo-programming@earth.lyris.net
===========================================



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]