
January 11th, 2006, 11:45 AM
| | | Multiple inheritance and access specifiers issue.
I have a following piece of code. The code was compiled using g++
class A
{
public :
virtual void fn() = 0;
};
class B: virtual private A
{
public:
void fn() { cout << "I m here\n"; }
};
class C : public virtual A
{
};
class D : private B, virtual public C
{
};
int main()
{
D *mod1 = new D;
A *mod2 = mod1;
mod2->fn();
/* The following commented code does not compile */
/*
D *dObj = new D;
dObj->fn();
*/
return 0;
}
In this code how is the function fn() accessible to mod2 given that
class D is privately inheriting the implemetation of fn() from B.
Should it not be that, since implementation of fn() is privately
inherited from B the compiler should give B::fn() not accessible error
?
When the commented code is compiled, the compiler gives the error. This
is expected.....
So, how in the first case the resolution of a function call being done ? | 
January 11th, 2006, 11:55 AM
| | | Re: Multiple inheritance and access specifiers issue.
Tapeesh wrote:[color=blue]
> I have a following piece of code. The code was compiled using g++
>
> class A
> {
> public :
> virtual void fn() = 0;
> };
>
> class B: virtual private A
> {
> public:
> void fn() { cout << "I m here\n"; }
> };
>
> class C : public virtual A
> {
> };
>
> class D : private B, virtual public C
> {
> };
>
> int main()
> {
> D *mod1 = new D;
> A *mod2 = mod1;
> mod2->fn();[/color]
In this case the mod2 is of type A*, where in which the fn() is defined
in the public section.[color=blue]
>
> /* The following commented code does not compile */
>
> /*
> D *dObj = new D;
> dObj->fn();[/color]
In this case the dObj is of type D* where the fn() is in the private
section. Because the class B derived private.[color=blue]
> */
> return 0;
> }
>
> In this code how is the function fn() accessible to mod2 given that
> class D is privately inheriting the implemetation of fn() from B.
> Should it not be that, since implementation of fn() is privately
> inherited from B the compiler should give B::fn() not accessible error
> ?
>
> When the commented code is compiled, the compiler gives the error. This
> is expected.....
>
> So, how in the first case the resolution of a function call being done ?[/color]
Virtual Fuctions assume the access level of the
object/pointer/reference through which they are called. | 
January 11th, 2006, 12:05 PM
| | | Re: Multiple inheritance and access specifiers issue.
Tapeesh wrote:
[color=blue]
> class A { public : virtual void fn() = 0;};
> class B: virtual private A { public: void fn() { cout << "I m here\n"; } };
> class C : public virtual A { };
> class D : private B, virtual public C { };[/color]
[color=blue]
> int main()
> {
> D *mod1 = new D;
> A *mod2 = mod1;
> mod2->fn();[/color]
mod2 is A* and fn() is public in class A. No compiler error.
[color=blue]
> /* The following commented code does not compile */
>
> /*
> D *dObj = new D;
> dObj->fn();[/color]
dObj is D* and 'fn' is private in B* and hence inaccessible for D*.
[color=blue]
> */
> return 0;
> }
>
> In this code how is the function fn() accessible to mod2 given that
> class D is privately inheriting the implemetation of fn() from B.
> Should it not be that, since implementation of fn() is privately
> inherited from B the compiler should give B::fn() not accessible error
> ?[/color]
Note that type checking is done at compile time, so when you say
mod2->fn(), the compiler has no way to understand that mod2 is A* but
the actual object pointed is D. | 
January 12th, 2006, 04:55 AM
| | | Re: Multiple inheritance and access specifiers issue.
Neelesh Bodas wrote:[color=blue]
> Tapeesh wrote:
>[color=green]
> > class A { public : virtual void fn() = 0;};
> > class B: virtual private A { public: void fn() { cout << "I m here\n"; } };
> > class C : public virtual A { };
> > class D : private B, virtual public C { };[/color]
>[color=green]
> > int main()
> > {
> > D *mod1 = new D;
> > A *mod2 = mod1;
> > mod2->fn();[/color]
>
> mod2 is A* and fn() is public in class A. No compiler error.
>[color=green]
> > /* The following commented code does not compile */
> >
> > /*
> > D *dObj = new D;
> > dObj->fn();[/color]
>
> dObj is D* and 'fn' is private in B* and hence inaccessible for D*.
>[color=green]
> > */
> > return 0;
> > }
> >
> > In this code how is the function fn() accessible to mod2 given that
> > class D is privately inheriting the implemetation of fn() from B.
> > Should it not be that, since implementation of fn() is privately
> > inherited from B the compiler should give B::fn() not accessible error
> > ?[/color]
>
> Note that type checking is done at compile time, so when you say
> mod2->fn(), the compiler has no way to understand that mod2 is A* but
> the actual object pointed is D.[/color]
This is fine that the compiler has no way to understand that mod2
is actually pointing to object of D.
But when this code is executed, how is a privately inherited
function accessible ? Because dynamic binding will resolve that mod2 is
actually of type D and then fn() is being called which is a private
function of D. So, at execution time there should be some error but the
privately inherited definition of fn() is getting executed. Or
1.) Is it that access specifiers are important only at compile time and
have no meaning during execution?
2.) This type of inheritance is providing a way for accessing private
members of a class? | 
January 12th, 2006, 06:05 AM
| | | Re: Multiple inheritance and access specifiers issue.
Tapeesh wrote:[color=blue]
> Neelesh Bodas wrote:[color=green]
> > Tapeesh wrote:
> >[color=darkred]
> > > class A { public : virtual void fn() = 0;};
> > > class B: virtual private A { public: void fn() { cout << "I m here\n"; } };
> > > class C : public virtual A { };
> > > class D : private B, virtual public C { };[/color]
> >[color=darkred]
> > > int main()
> > > {
> > > D *mod1 = new D;
> > > A *mod2 = mod1;
> > > mod2->fn();
> > > return 0;
> > > }
> > >[/color][/color]
>[/color]
[color=blue]
> This is fine that the compiler has no way to understand that mod2
> is actually pointing to object of D.
>[/color]
[color=blue]
> But when this code is executed, how is a privately inherited
> function accessible ?[/color]
Execution knows nothing about access specifiers, the actual assembly
code simply goes at the proper offset within the class.
[color=blue]
> Because dynamic binding will resolve that mod2 is actually of type D and then fn() is[/color]
No. Dynamic binding doesnot resolve the "types" - type checking is
always done at compile time. What dynamic binding means is that the
name-value association (aka, answer to the question "Which definition
does this name correspond to") is delayed till run time.
Also note that the term "dynamic binding" has nothing to do with the
term "Dynamic type checking" - these two are different concepts.
[color=blue]
> being called which is a private
> function of D. So, at execution time there should be some error but the
> privately inherited definition of fn() is getting executed. Or
>[/color]
Access specifiers, Types, function prototypes, existence of a "class"
and a friend, and most of the other langauge constructs are known only
to the compiler.
[color=blue]
> 2.) This type of inheritance is providing a way for accessing private
> members of a class?[/color]
Not really - fn() was public in B . Just make fn() private in B and
don't be surprised if you get linker errors. | 
January 12th, 2006, 07:25 AM
| | | Re: Multiple inheritance and access specifiers issue.
Neelesh Bodas wrote:[color=blue]
> Tapeesh wrote:[color=green]
> > Neelesh Bodas wrote:[color=darkred]
> > > Tapeesh wrote:
> > >
> > > > class A { public : virtual void fn() = 0;};
> > > > class B: virtual private A { public: void fn() { cout << "I m here\n"; } };
> > > > class C : public virtual A { };
> > > > class D : private B, virtual public C { };
> > >
> > > > int main()
> > > > {
> > > > D *mod1 = new D;
> > > > A *mod2 = mod1;
> > > > mod2->fn();
> > > > return 0;
> > > > }
> > > >[/color]
> >[/color]
>[color=green]
> > This is fine that the compiler has no way to understand that mod2
> > is actually pointing to object of D.
> >[/color]
>[color=green]
> > But when this code is executed, how is a privately inherited
> > function accessible ?[/color]
>
> Execution knows nothing about access specifiers, the actual assembly
> code simply goes at the proper offset within the class.
>[color=green]
> > Because dynamic binding will resolve that mod2 is actually of type D and then fn() is[/color]
>
>
> No. Dynamic binding doesnot resolve the "types" - type checking is
> always done at compile time. What dynamic binding means is that the
> name-value association (aka, answer to the question "Which definition
> does this name correspond to") is delayed till run time.
>
> Also note that the term "dynamic binding" has nothing to do with the
> term "Dynamic type checking" - these two are different concepts.
>
>[color=green]
> > being called which is a private
> > function of D. So, at execution time there should be some error but the
> > privately inherited definition of fn() is getting executed. Or
> >[/color]
>
> Access specifiers, Types, function prototypes, existence of a "class"
> and a friend, and most of the other langauge constructs are known only
> to the compiler.
>[color=green]
> > 2.) This type of inheritance is providing a way for accessing private
> > members of a class?[/color]
>
> Not really - fn() was public in B . Just make fn() private in B and
> don't be surprised if you get linker errors.[/color]
I tried with fn() private in class B. It has no effect. There is no
compiler error (just a warning that B has all its members functions
private) or any run time error. But making fn() private in A gives a
compile time error, which is as expected.
So, this means that
This type of inheritance is providing a way for accessing private
members of a class? | 
January 12th, 2006, 01:15 PM
| | | Re: Multiple inheritance and access specifiers issue.
Tapeesh wrote:[color=blue][color=green]
> >
> > Not really - fn() was public in B . Just make fn() private in B and
> > don't be surprised if you get linker errors.[/color]
>
> I tried with fn() private in class B. It has no effect. There is no
> compiler error (just a warning[/color]
I see. Yeah it will work since B-subobject of D has fn() defined. I had
changed the code a bit and didnot remember that, hence got linking
errors.
[color=blue]
> So, this means that
> This type of inheritance is providing a way for accessing private
> members of a class?[/color]
No idea. Somebody else, please comment on this. | 
January 12th, 2006, 04:05 PM
| | | Re: Multiple inheritance and access specifiers issue.
[color=blue]
> No idea. Somebody else, please comment on this.[/color]
Luck? The private fn()-method of B is probably at the right place
in the vtable of the object.
Does it still work if you define
class D : virtual public C, private B
{
};
?
rs | 
January 13th, 2006, 02:45 AM
| | | Re: Multiple inheritance and access specifiers issue.
Neelesh Bodas wrote:[color=blue]
> Tapeesh wrote:[color=green][color=darkred]
> > >
> > > Not really - fn() was public in B . Just make fn() private in B and
> > > don't be surprised if you get linker errors.[/color]
> >
> > I tried with fn() private in class B. It has no effect. There is no
> > compiler error (just a warning[/color]
>
> I see. Yeah it will work since B-subobject of D has fn() defined. I had
> changed the code a bit and didnot remember that, hence got linking
> errors.
>[color=green]
> > So, this means that
> > This type of inheritance is providing a way for accessing private
> > members of a class?[/color]
>
> No idea. Somebody else, please comment on this.[/color]
Hi,
IMO, I believe this is more to conceptual issue rather than technical
issue.
If your grandaddy says, his motorcycle should be given TO ANYONE, and
then your father keep it as his own, without giving to anyone, but your
mother disagree, and still give it TO ANYONE. Then when your time
comes, you keep the motorcycle (you believe the motorcycle should be
give & not give), but then someone ask your grandfather (not you) for
his motorcycle. Guess, what your grandpa says? GIVE IT TO THEM or i
take it back! why? because grandpa already says PUBLIC. Got it?? :-)
Maybe this can be seen as alternative (back door) to 'private'd method.
But i rather believe this is natural, because originally, the function
already defined as public. If the fn() defined as private in A, that's
another thing. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|