Function calls whenever an object is accessed. . .  | | |
January 9th, 2006, 03:24 PM
|
#1 (permalink)
| | Member
Join Date: Sep 2004 Location: Your living room...
Posts: 417
| Function calls whenever an object is accessed. . .
Is anyone familiar with any language that provides a function call similar to a constructor, but that get called EVERY time the object is accessed; not just on initialization? I mean, I know I could simply manually add a specific function call to every function, but I am hoping for some existing implementation that has something available already.
__________________
I reserve the right to contradict myself. . .
|
| |
January 11th, 2006, 12:45 PM
|
#2 (permalink)
| | Banned
Join Date: Jan 2004 Location: Earth
Posts: 420
|
I have never heard of anything like that before, but if you had one main function that called the other functions in the object then since every call had to go through main first you could add the function you wanted called everytime there... |
| |
January 11th, 2006, 12:56 PM
|
#3 (permalink)
| | Member
Join Date: Sep 2004 Location: Your living room...
Posts: 417
| need "self-aware"objects. . .
Well, I understand that I can create a function, then manually have every other function call that one before it itself executes, but that isn't what I'm looking for. I need self-aware objects that can call a specific function every single time the object itself is accessed. Sort of like an __init__ during object initialization, except this one is called EVERY time the object is accessed; not just during creation. |
| |
January 11th, 2006, 01:58 PM
|
#4 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
That's kind of confusing, at least to me...
Class Points {
private:
int x, y, r, l, w, h;
public:
Points::Points{ (x = 0, y = 0, r = 0, l = 0, w = 0, h = 0) }
Points::draw_circle(int x, int y, int r) {}
Points::draw_square(int x, int y) {}
};
void main(void) {
int x = 4, y =5, r = 2;
Points my_points; // Constructor called here
my_points.draw_circle(x, y, r); // WHAT DO YOU WANT CALLED???
} |
| |
January 11th, 2006, 02:08 PM
|
#5 (permalink)
| | Member
Join Date: Sep 2004 Location: Your living room...
Posts: 417
| Quote: |
Originally Posted by Rootstonian That's kind of confusing, at least to me...
| I want: Code: Class Points {
private:
int x, y, r, l, w, h;
void alwayscalledwhenaccessed;
public:
Points::Points{ (x = 0, y = 0, r = 0, l = 0, w = 0, h = 0) }
Points::draw_circle(int x, int y, int r) {}
Points::draw_square(int x, int y) {}
};
void main(void) {
int x = 4, y =5, r = 2;
Points my_points; // Constructor called here
my_points.draw_circle(x, y, r); //I want alwayscalledwhenaccessed called here
my_points.draw_square(x,y); //and here. . .
} . . .just as the powers that be *know* to call the contructor (although the call stack would obviously be different) I want it to simply *know* to call the allwayscallwhenaccessed method. I'm curious as to whether any languages offer this. |
| |
January 11th, 2006, 02:24 PM
|
#6 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
Wow, that's a trip!
I'm a bit rusty on this, but how is the Points Constructor called? I imagine it's done behind the scenes at the instantiation, but still a "manual" process (someone coded *something* there).
I just don't see it, unless you do as you DON'T want to have:
"this->alwayscalledwhenaccessed();" in each method...sorry dude...my brain is toast at this point of the programming work day  |
| |
January 11th, 2006, 02:31 PM
|
#7 (permalink)
| | Member
Join Date: Sep 2004 Location: Your living room...
Posts: 417
| Quote: |
Originally Posted by Rootstonian Wow, that's a trip!
I'm a bit rusty on this, but how is the Points Constructor called? I imagine it's done behind the scenes at the instantiation, but still a "manual" process (someone coded *something* there).
. . . |
Right, I'm sure it is *coded* in there somewhere. But that's exactly what I'm asking: If they *coded* the ability to call a contructor/destructor, it certainly seems possible to have coded an intermediary of sorts that gets called in process whenever the object is accessed. Whether anyone has actually done this or not is my concern. It would really be nice if something existed already. |
| |
January 11th, 2006, 02:37 PM
|
#8 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
How and when a constructor is called depends on the language. Some call it for you (Java) others require a call to the constructor in the code. In java it goes "Oh you declared a variable, certainly you want the constructor" and it does it for you.
C++ on the other hand makes no such assumption and you have to call the constructor yourself or you will simply have garbage data and unitialized variables. IIRC this is all done in the compiler.
As far as getting this done though, the only chance you have of actually accomplishing this would be to try and register an event for accessing. It would be better just to call the function from each member function. There is no language to my knowledge with "Aware" objects. You would need to code this awareness into an object.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
January 11th, 2006, 02:47 PM
|
#9 (permalink)
| | Member
Join Date: Sep 2004 Location: Your living room...
Posts: 417
| Quote: |
Originally Posted by jkrohn How and when a constructor is called depends on the language.
. . .
As far as getting this done though, the only chance you have of actually accomplishing this would be to try and register an event for accessing. It would be better just to call the function from each member function. There is no language to my knowledge with "Aware" objects. You would need to code this awareness into an object.
Jkrohn | Yeah, I'm aware that different languages handle it differently. I was just hoping for one that, like Java, assumed responsibility and triggered certain methods (similar to constructors/destructors) on the fly.
I was just discussing it with someone in the office, and they recommended "aspect-oriented" programming; specifically AspectJ. I'll check into it and see if that is of any use.
Thanks for the input so far! |
| |
January 12th, 2006, 11:42 AM
|
#10 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
In thinking some more about this, it really sounds like what Windows programming does. It is message based; mouse clicks, keyboard hits etc. are are put into a message queue and processed. In this environment, your INIT routine could be called on every mouse click, or mouse move or key click or windows min/maximize or every clock tick for that matter (last I knew, that was 18.2 times per second  ) |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |