Skip to main content
gamedev.net
🔒 Locked

C++ programmer wanting inner classes.

Started by PsiRadish May 14, 2004 at 1:56 PM 9 replies 450+ views
Original Post
PsiRadish
PsiRadish
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++?
This world is not shaped by vague metaphysical forces. It is not God who kills the children. Not fate that butchers them or destiny that feeds them to the dogs. It's us. Only us.
Xai
Xai
the 2 things in C++ that slightly relate to this are:

protected access levels instead of private.

nested classes.
PsiRadish
PsiRadish
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]
This world is not shaped by vague metaphysical forces. It is not God who kills the children. Not fate that butchers them or destiny that feeds them to the dogs. It's us. Only us.
SiCrane
SiCrane
Or you could just use function pointers or function objects.
PsiRadish
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.
This world is not shaped by vague metaphysical forces. It is not God who kills the children. Not fate that butchers them or destiny that feeds them to the dogs. It's us. Only us.
Oluseyi
Oluseyi
quote:
Original post by PsiRadish
Has anyone found a good way to mimmick this functionality in C++?
Functors and function pointers are about as close as you get. Note that you can not define them "inline" as you can do with Java''s anonymous classes (used in event handling).
Oluseyi
Oluseyi
quote:
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.
IIRC, inner functors are like other class members and thus have access to private members.
SiCrane
SiCrane
You can pass a function with sufficient access, be it a friend function or a member function as a function pointer.

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.
mfawcett
mfawcett
quote:
Original post by Oluseyi
quote:
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.
IIRC, inner functors are like other class members and thus have access to private members.

Only in broken compilers. Inner classes/function objects/structs have no special rights, only an extra scope (the outer class).

--Michael Fawcett
PsiRadish
PsiRadish
I think I get what you''re saying, SiCrane, and I think I may like it.
This world is not shaped by vague metaphysical forces. It is not God who kills the children. Not fate that butchers them or destiny that feeds them to the dogs. It's us. Only us.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.