 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Anis Benyelloul Guest
|
Posted: Tue Dec 30, 2003 12:57 am Post subject: Selective Friendly: Choose what you want to share ! |
|
|
I'm developping a game engine, and I developed two classes that are closely
linked to each-other but neither of them is the 'parent' of the other
....
class Sprite { //Animated object
/....
protcted:
void draw(Painter&, Rectangle& clip);
/...
};
class Application
{
void run()
{
//....
for( /*All the sprites ... */){
i->draw(screen, clip);
}
}
};
The problem is that draw is protected, and must not be called by other classes
then application. I first thought of linking Sprite and Appliaction with a
firendly link,... but Sprite contains things that must not be accessed by
Application,... Application needs a special pass to access only draw ......
Here is the sollution I adopted ...
class Sprite {
//....
protected:
void draw(Painter&, Rectangle& clip);
public:
template <class T> AccessProxy{};
// Privileages of Application
friend class AccessProxy<Application>
class AccessProxy<Application>
{
friend class Application;
static void draw(Sprite* This, Painter&p, Rectaangle& clip)
{ return This->draw(p, clip);}
};
//Privileages of ....
friend class AccessProxy<..> //...
class AccessProxy<...> //...
};
Now if some Application's member wants to access draw() it must do :
Application::run()
{
Sprite* current;
//.....
AccessProxy<Application>::draw(current, screen ,clip);
}
main() {
Sprite * test;
/*....*/
test->draw(..); //error !!
AccessProxy<Application>::draw(..); //error this is private ...
}
I think this is a good sollution since it can be easily generalised to
other classes, it is as fast as normal member call, (see how I
emulated what the compiler normaly do through the 'this' pointer ..)
Also it permits to choose exactly what to show to who ....
If some noe has some comments improvements or other suggestions I'll
be very happy to hear them ...
PS:I'm writting this in a hurry so pardon me typo e_r_ors , if any ...
:)
Thanks in advence ...
Happy new year ,..... god save C++ !!!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Tue Dec 30, 2003 12:40 pm Post subject: Re: Selective Friendly: Choose what you want to share ! |
|
|
"Anis Benyelloul" <benyelloul (AT) algeriamail (DOT) com> wrote
| Quote: | I'm developping a game engine, and I developed two classes that are
closely
linked to each-other but neither of them is the 'parent' of the other
...
class Sprite { //Animated object
/....
protcted:
void draw(Painter&, Rectangle& clip);
/...
};
class Application
{
void run()
{
//....
for( /*All the sprites ... */){
i->draw(screen, clip);
}
}
};
The problem is that draw is protected, and must not be called by other
classes
then application. I first thought of linking Sprite and Appliaction with a
firendly link,... but Sprite contains things that must not be accessed by
Application,... Application needs a special pass to access only draw
.......
|
Sprite can grant friendship to Application::run.
As the author of Application, why are you worried that Application will
misuse its friendship privileges? Are you afraid that someone will later
modify Application to do something improper?
Jonathan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anis Benyelloul Guest
|
Posted: Wed Dec 31, 2003 9:31 pm Post subject: Re: Selective Friendly: Choose what you want to share ! |
|
|
| Quote: |
Sprite can grant friendship to Application::run.
As the author of Application, why are you worried that Application will
misuse its friendship privileges? Are you afraid that someone will later
modify Application to do something improper?
|
So ? this means I can write things like
class A
{
friend B::run;
};
Do this works also on typdefs, object members, ... etc..
Really ? Jeez !! how stupid of me !!!
The book I learnd C++ on didn't mention that !!, or perhaps this is a
new feature of th langage and my book is a bit out dated ....
Thanks, really ,.....
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Thu Jan 01, 2004 2:03 am Post subject: Re: Selective Friendly: Choose what you want to share ! |
|
|
"Anis Benyelloul" <benyelloul (AT) algeriamail (DOT) com> wrote in message
| Quote: | Do this works also on typdefs, object members, ... etc..
Really ? Jeez !! how stupid of me !!!
The book I learnd C++ on didn't mention that !!, or perhaps this is a
new feature of th langage and my book is a bit out dated ....
Thanks, really ,.....
|
Do I detect some sarcasm here!? I didn't mean to insult your intelligence.
Your technique does seem to be correct, and it is not exactly the same as
declaring a member function friend. However, I am not convinced of its
usefulness. Usually when I have had trouble expressing a friendship relation
in C++ the problem is that the existing C++ mechanisms would extend access
rights to far. E.g., I can't say
template<typename T>
class S {
friend class T;
/* ... */
};
and making the members which T needs to access public might be dangerous.
In the case you describe, however, I don't understand why Sprite can't grant
friendship to Application.You have complete control over Application, and
can make sure it doesn't do anything bad. If the internals of Sprite are so
delicate that they are difficult to use correctly, maybe they should be
further encapsulated. After all, any misuse of the Sprite internals by
Application could just as easily occur within the code of Sprite itself.
No hard feelings?
Jonathan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|