C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

access rights: frienship and inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
radugrigore@gmail.com
Guest





PostPosted: Tue Jan 18, 2005 10:59 pm    Post subject: access rights: frienship and inheritance Reply with quote



I have four classes:

class A {};
class B : public A {};
class C : public A { private: int x; };
class D : public C {};

The member "x" has some rather strange access requirements:

a. It should be accessible from class C; that's where its content is
computed.
b. It should be read by _one_ method from D and _one_ method from B.
c. No other methods from D or B or A or another class in the project
need to access it.

Right now classes D and B are declared as friends of C. As a result I
wrote by accident code that reads x from within D. The bug went
undetected for about one week. It would be nice if (c) could be
enforced at compile time.

I have tried:

class C : public A {
private: int x;
friend void D::SpecialFunction();
};

class D : public C {
void SpecialFunction() { x; }
void NormalFunction(); { x; } // I want an error here
};

But D::SpecialFunction cannot be declared as friend if D has incomplete
type and D cannot be defined if C has incomplete type Sad
Is there any way around it?

regards,
radu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
igor.tanchovski@gmail.com
Guest





PostPosted: Wed Jan 19, 2005 1:18 pm    Post subject: Re: access rights: frienship and inheritance Reply with quote



You may declare SpecialFunc a friend function to both classes.


class A {} ;

class D ;
class B : public A {};
class C : public A {
friend void SpecialFunc(D *pd) ;
private: int x;
};

class D : public C
{
friend void SpecialFunc(D *pd) ;
//somting
};

void SpecialFunc(D *pd)
{
//somting
pd->x = 5 ;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dave Moore
Guest





PostPosted: Wed Jan 19, 2005 11:16 pm    Post subject: Re: access rights: frienship and inheritance Reply with quote




<radugrigore (AT) gmail (DOT) com> wrote

Quote:
I have four classes:

class A {};
class B : public A {};
class C : public A { private: int x; };
class D : public C {};

The member "x" has some rather strange access requirements:

a. It should be accessible from class C; that's where its content is
computed.
b. It should be read by _one_ method from D and _one_ method from B.
c. No other methods from D or B or A or another class in the project
need to access it.

Why not simply provide a const accessor for x within C? As in:

class C : public A {
private:
int _x;
public:
int x() const {return _x;}
};

Quote:
Right now classes D and B are declared as friends of C.

This is not desirable in any case, because you only want to provide *read*
access to D and B, but making them friends provides them with full access.
The accessor function I suggested above provides read-only access to
everyone, not just B and D, but why do you want to restrict read access
anyway?

Quote:
As a result I wrote by accident code that reads x from within D.

I guess you meant "writes x" in the above sentence?

Quote:
The bug went undetected for about one week. It would be nice if (c)
could be enforced at compile time.

Well, my suggestion above will at least allow you to more easily identify an
attempt to read the value of x from a object of type C (or D). And if you
really did mean "reads x" above, that sounds like a problem that could
probably be fixed fairly easily with a more careful choice of variable
names.

Quote:
I have tried:

class C : public A {
private: int x;
friend void D::SpecialFunction();
};
class D : public C {
void SpecialFunction() { x; }
void NormalFunction(); { x; } // I want an error here
};

But D::SpecialFunction cannot be declared as friend if D has incomplete
type and D cannot be defined if C has incomplete type Sad
Is there any way around it?

There are certainly workarounds available to obtain exactly the behavior you
describe using the "most problems can be solved by adding a layer of
abstraction" rule, but the only ones I could come up with are absurdly ugly,
complicated and contrived to fit the current example (so much so that I
decided not to post them). My intuition is that you have "designed yourself
into a corner" (I do it all the time), and you will have to "unwind" the
design to the critical point and go forward from there. However, without
more details it is impossible to provide more help.

HTH,

Dave Moore



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
M Jared Finder
Guest





PostPosted: Wed Jan 19, 2005 11:56 pm    Post subject: Re: access rights: frienship and inheritance Reply with quote

[email]radugrigore (AT) gmail (DOT) com[/email] wrote:
Quote:
I have four classes:

class A {};
class B : public A {};
class C : public A { private: int x; };
class D : public C {};

The member "x" has some rather strange access requirements:

a. It should be accessible from class C; that's where its content is
computed.
b. It should be read by _one_ method from D and _one_ method from B.
c. No other methods from D or B or A or another class in the project
need to access it.

You'll need to create a new class that represent the access you want for
D and B. D and B will have to use this class to access the member x.
For example, this compiles just fine on g++ 3.3:

class C {
friend class C_x;
private:
int x;
};

class C_x {
friend class D;
private:
static int x( const C& c ) { return c.x; }
static void x( C& c, int x) { c.x = x; }
};

class D : public C {
public:
void func()
{
C_x:Mad( *this, C_x:Mad( *this ) + 1 );
}
};

-- MJF

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.