473,498 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ?

Jan 11 '06 #1
8 2424

Tapeesh wrote:
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(); In this case the mod2 is of type A*, where in which the fn() is defined
in the public section.
/* The following commented code does not compile */

/*
D *dObj = new D;
dObj->fn(); In this case the dObj is of type D* where the fn() is in the private
section. Because the class B derived private. */
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 ?


Virtual Fuctions assume the access level of the
object/pointer/reference through which they are called.

Jan 11 '06 #2
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();
mod2 is A* and fn() is public in class A. No compiler error.
/* The following commented code does not compile */

/*
D *dObj = new D;
dObj->fn();
dObj is D* and 'fn' is private in B* and hence inaccessible for D*.
*/
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
?


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.

Jan 11 '06 #3
Neelesh Bodas wrote:
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();


mod2 is A* and fn() is public in class A. No compiler error.
/* The following commented code does not compile */

/*
D *dObj = new D;
dObj->fn();


dObj is D* and 'fn' is private in B* and hence inaccessible for D*.
*/
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
?


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.


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?

Jan 12 '06 #4
Tapeesh wrote:
Neelesh Bodas wrote:
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;
}

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 ?
Execution knows nothing about access specifiers, the actual assembly
code simply goes at the proper offset within the class.
Because dynamic binding will resolve that mod2 is actually of type D and then fn() is

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.

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

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.
2.) This type of inheritance is providing a way for accessing private
members of a class?


Not really - fn() was public in B . Just make fn() private in B and
don't be surprised if you get linker errors.

Jan 12 '06 #5

Neelesh Bodas wrote:
Tapeesh wrote:
Neelesh Bodas wrote:
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;
> }
>

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 ?


Execution knows nothing about access specifiers, the actual assembly
code simply goes at the proper offset within the class.
Because dynamic binding will resolve that mod2 is actually of type D and then fn() is

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.

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


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.
2.) This type of inheritance is providing a way for accessing private
members of a class?


Not really - fn() was public in B . Just make fn() private in B and
don't be surprised if you get linker errors.


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?

Jan 12 '06 #6

Tapeesh wrote:

Not really - fn() was public in B . Just make fn() private in B and
don't be surprised if you get linker errors.
I tried with fn() private in class B. It has no effect. There is no
compiler error (just a warning


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.
So, this means that
This type of inheritance is providing a way for accessing private
members of a class?


No idea. Somebody else, please comment on this.

Jan 12 '06 #7
No idea. Somebody else, please comment on this.


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

Jan 12 '06 #8

Neelesh Bodas wrote:
Tapeesh wrote:

Not really - fn() was public in B . Just make fn() private in B and
don't be surprised if you get linker errors.


I tried with fn() private in class B. It has no effect. There is no
compiler error (just a warning


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.
So, this means that
This type of inheritance is providing a way for accessing private
members of a class?


No idea. Somebody else, please comment on this.


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.

Jan 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4358
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
30
2672
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
22
23312
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
8
1449
by: mmu2643 | last post by:
Hi, I had a question regarding multiple inheritance. class B1{ public: int a; };
47
3587
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4862
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
14
3571
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
2
2022
by: Alexander Adam | last post by:
Hi! I got a class structure similar to: class Base_Object { ... some functions ... } class Object: public Base_Object,
3
2532
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.