PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > access modifier in c++
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
access modifier in c++

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2008, 09h59   #1
toton.basak@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut access modifier in c++

In C++ any i can simulate some restricted access modifiers based on
association & inheritance?
eg,
class BombMaker{
private:
void makeAtomBomb(){
std::cout<<"making atom bomb\n";
}
int getSecretFormula()const{return 51;}
protected:
void makeRDX(){
std::cout<<"making RDX\n";
}
int getRDXFormula()const{return 101;}
std::vector<BombMaker*> ers;
public:
void makeFireCracker(){
std::cout<<"making firecrackers\n";
}
void makeAtomBombFor(BombMaker& m){
m.makeAtomBomb();
}
void stealFormula(BombMaker& o){
int formula = o.getSecretFormula();
}
};

here,
BombMaker* bad = new BombMaker();
BombMaker* notSoBad = new BombMaker();
bad->makeAtomBombFor(*notSoBad);/// i want this to work as now it
works. so another bomb maker can give the bad one to make a bomb for
him.
bad->stealFormula(*notSoBad);/// but bad one even can steal the
formula for the bomb through association with notSoBad. That should
not be allowed as the formula is only for personal use and is
dangerous for world. Can private be made exclusive so that association
is not allowed for private?
ie, essentially getSecretFormula() can only be used by the object
itself.

secondly,
i have a derived class,
class DestructiveBuddy : public BombMaker{
public:
void getFormula(BombMaker& o){//1
int formula = o.getRDXFormula();
}
void getFormulaFrom(DestructiveBuddy o){
int formula = o.getRDXFormula();
}
void acquireFormula(){
int formula = getRDXFormula();
}
void getFromers(){//2
for(std::size_t i = 0; i< ers.size();++i){
ers[i]->getRDXFormula();
}
}
};
now is there any way to have a protected modifier which allows an
DestructiveBuddy to getFromula from a BombMaker also (as he himself is
a BombMaker) as in (1) along with other DestructiveBuddy's.
In the same way, an DestructiveBuddy object also want's all of the
er which it has, to give the RDXFormula to him as all of them are
BombMaker and so is DestructiveBuddy himself, but not to anyone who is
not a BombMaker.
So unlike the previous case, where private through association is not
required, here protected through association is require.

Thanks



  Réponse avec citation
Vieux 16/07/2008, 10h43   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: access modifier in c++

* toton.basak@gmail.com:
> In C++ any i can simulate some restricted access modifiers based on
> association & inheritance?
> eg,
> class BombMaker{
> private:
> void makeAtomBomb(){
> std::cout<<"making atom bomb\n";
> }
> int getSecretFormula()const{return 51;}
> protected:
> void makeRDX(){
> std::cout<<"making RDX\n";
> }
> int getRDXFormula()const{return 101;}
> std::vector<BombMaker*> ers;
> public:
> void makeFireCracker(){
> std::cout<<"making firecrackers\n";
> }
> void makeAtomBombFor(BombMaker& m){
> m.makeAtomBomb();
> }
> void stealFormula(BombMaker& o){
> int formula = o.getSecretFormula();
> }
> };
>
> here,
> BombMaker* bad = new BombMaker();
> BombMaker* notSoBad = new BombMaker();
> bad->makeAtomBombFor(*notSoBad);/// i want this to work as now it
> works. so another bomb maker can give the bad one to make a bomb for
> him.
> bad->stealFormula(*notSoBad);/// but bad one even can steal the
> formula for the bomb through association with notSoBad. That should
> not be allowed as the formula is only for personal use and is
> dangerous for world. Can private be made exclusive so that association
> is not allowed for private?
> ie, essentially getSecretFormula() can only be used by the object
> itself.


Simply don't expose that functionality via any protected or public member function.


> secondly,
> i have a derived class,
> class DestructiveBuddy : public BombMaker{
> public:
> void getFormula(BombMaker& o){//1
> int formula = o.getRDXFormula();
> }
> void getFormulaFrom(DestructiveBuddy o){
> int formula = o.getRDXFormula();
> }
> void acquireFormula(){
> int formula = getRDXFormula();
> }
> void getFromers(){//2
> for(std::size_t i = 0; i< ers.size();++i){
> ers[i]->getRDXFormula();
> }
> }
> };
> now is there any way to have a protected modifier which allows an
> DestructiveBuddy to getFromula from a BombMaker also (as he himself is
> a BombMaker) as in (1) along with other DestructiveBuddy's.


No, not for direct access. However, DestructiveBuddy can access the protected
getRDXFormula of a BombMaker via a member function pointer. Member function
pointers are about on the level of goto: don't use unless you absolutely need
to, because there's very little protection.

class DestructiveBuddy : public BombMaker{
private:
static int rdxFormulaFrom( BombMaker& o )
{
int (BombMaker::*getter)() const = &DestructiveBuddy::getRDXFormula;
return (o.*getter)();
}
public:
void getFormula(BombMaker& o){//1
int formula = rdxFormulaFrom( o );
}
void getFormulaFrom(DestructiveBuddy o){
int formula = o.getRDXFormula();
}
void acquireFormula(){
int formula = getRDXFormula();
}
void getFromers(){//2
for(std::size_t i = 0; i< ers.size();++i){
rdxFormulaFrom( *ers[i] );
}
}
};

Note that this is only technical information about what you're asking.

If you do use this for gaining access, you're doing yourself (or someone) a
great disservice -- instead design the classes with proper access! In
considering this advice, take into account that you had to ask and I knew an
answer. Therefore, chances are that this advice about avoiding member function
pointers (for this purpose, and generally direct use of them) is well-founded.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10122 seconds with 10 queries