Topic: Uses of class methods in particular cases...
Author: David Apfelbaum <da0g+@andrew.cmu.edu>
Date: Fri, 24 Mar 1995 23:09:55 -0500 Raw View
This would work. But it's a real hack...
And perhaps this does belong on comp.std.c++, since this solution
*really* should not be allowed. It works by means of doing funky
pointer arithmetic inside of the subclass...
ie, either doing "((b *) & a)->a::do()" should be forbidden inside class
B, or it should be possible to access protected members in a instance of
that class inside of a subclass...
Granted, you could make class B a friend of class A, but there are
occasions where you really don't want to have to modify class A to do
this...
Example:
class A
{
...
protected: // Make do() a protected member, only this class
// & subclasses can access do().
void do();
...
};
class B : public A // This can be public, protected, or private inheritance
{
...
public :
...
void doit()
{ A a; // Make this a instance of A, not a pointer, for clarity.
((b *) & a)->a::do(); // This is allowed, and actually works...
};
...
};
But
void main()
{
A* a;
a->do(); // This is forbidden at compile time.
}
Author: joffrin@irisa.fr (David Joffrin)
Date: 17 Mar 1995 17:29:43 GMT Raw View
Hello,
I have a problem with the method of the class A... Let me give you an example :
class A
{
...
void do();
...
};
class B : public A
{
...
public :
...
void doit()
{ A* a;
a->do(); I want to allow this situation...
};
...
};
But
void main()
{
A* a;
a->do(); I want to forbide this situation...
So, I don't want to allow to use the do() method of the class A, except if I am in the classes A or B (even if, I use an instance of A in this classes, like in the example).
I want to know how I can implement the do() method of the class A to have this kind of particular situation.
I expect you understand this strange case...
Thanks in advance.
DAVID.