🔒 Locked
C++ programmer wanting inner classes.
Advertisement
Original Post
I''ve been learning Java and C++, and while I consider C++ superior for everything I want to do with programming (pretty much just make games), I do sorely miss Java''s inner classes. Not exactly the inner classes themselves, but the ability they give me to define a function "on the fly" for the behavior of, say, a button, with full access to private/protected members of the "outer" class. Has anyone found a good way to mimmick this functionality in C++?
I'm aware I could subclass my buttons like crazy, but I'd rather not have a seperate class for each and every button, it seems messy. What I'm considering is having buttons, when they're clicked, call a function of whatever window-type-thing they're on, sending a pointer to themselves so the window knows which button was clicked. Then I just have to subclass all my window-type-things with their own implementation of buttonClickedDoStuff(Button *button), which isn't too bad. Still not quite as easy as inner classes would make it, though :/
EDIT: Although now that I think about it, even with inner classes it would probably be best to make each window-type-thing its own subclass. So nevermind, I guess
[edited by - PsiRadish on May 14, 2004 3:49:56 PM]
EDIT: Although now that I think about it, even with inner classes it would probably be best to make each window-type-thing its own subclass. So nevermind, I guess
[edited by - PsiRadish on May 14, 2004 3:49:56 PM]
quote:IIRC, inner functors are like other class members and thus have access to private members.
Original post by PsiRadish
You can''t make a function pointer friend to my knowledge, so the function wouldn''t have sufficient access. Can a function object be given sufficient access? I have no experience with them.
You can pass a function with sufficient access, be it a friend function or a member function as a function pointer.
ex:
In this case it would probably be easier to use a function object rather than manipulating member function pointers directly, but the basic idea is the same.
ex:
class Window {
public:
void twiddle(void) {
data_++;
}
private:
int data_;
};
class Button {
public:
Button(Window * w, void (Window::*mfn)(void)) : w_(w), mfn_(mfn) {}
void on_click(void) {
(w_->*mfn_)();
}
private:
Window * w_;
void (Window::*mfn_)(void);
};
void some_func(void) {
Window w;
Button b(&w, &Window::twiddle);
b.on_click();
}
In this case it would probably be easier to use a function object rather than manipulating member function pointers directly, but the basic idea is the same.
quote:
Original post by Oluseyiquote:IIRC, inner functors are like other class members and thus have access to private members.
Original post by PsiRadish
You can''t make a function pointer friend to my knowledge, so the function wouldn''t have sufficient access. Can a function object be given sufficient access? I have no experience with them.
Only in broken compilers. Inner classes/function objects/structs have no special rights, only an extra scope (the outer class).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement