473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Inheritance and virtual functions

s
class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?

Does the answer depend on how I inherit classA from classB (i.e. public,
protected, private)?

Jul 22 '05 #1
14 1287
s wrote:
class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?
Yes.
Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?


No.

Jul 22 '05 #2

"s" <youshouldbe@home> wrote in message news:3FFDC248.5070204@home...
class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?
Of course. What do you have in mind that might be stopping you?
Does the answer depend on how I inherit classA from classB (i.e. public,
protected, private)?


Yes - you won't be able to access it externally if B uses private
inheritance.
Jul 22 '05 #3
On Thu, 08 Jan 2004 16:08:59 -0500, jeffc wrote:
Does the answer depend on how I inherit classA from classB (i.e. public,
protected, private)?


Yes - you won't be able to access it externally if B uses private
inheritance.


No, classA will not be accessable to users of classB, but that is
irrelevant for the question.

HTH,
M4

Jul 22 '05 #4

"s" <youshouldbe@home> skrev i en meddelelse news:3FFDC248.5070204@home...
class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?
Yes - but I do not see any practical reason to do so.

Does the answer depend on how I inherit classA from classB (i.e. public,
protected, private)?

No.

Remember to have a virtual destructor in A.

Kind regards
Peter
Jul 22 '05 #5
Rolf Magnus wrote:
s wrote:

class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?

Yes.


Right-o!
Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?

No.


I believe it does. Try this in the standard-compliant (ha) compiler of
your choice:

class A {
virtual void my_function() { }
}

class B: A {
void my_function() { A::myfunction( ); }
}

int main( )
{
A a;
}

Jul 22 '05 #6

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl...
On Thu, 08 Jan 2004 16:08:59 -0500, jeffc wrote:
Does the answer depend on how I inherit classA from classB (i.e. public, protected, private)?


Yes - you won't be able to access it externally if B uses private
inheritance.


No, classA will not be accessable to users of classB, but that is
irrelevant for the question.


He asked if the answer depends on how he inherits classA from classB. The
answer is yes. classA will not be accessible to users of classB.
Jul 22 '05 #7

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl...
On Thu, 08 Jan 2004 16:08:59 -0500, jeffc wrote:
> Does the answer depend on how I inherit classA from classB (i.e. public,> protected, private)?

Yes - you won't be able to access it externally if B uses private
inheritance.


No, classA will not be accessable to users of classB, but that is
irrelevant for the question.


He asked if the answer depends on how he inherits classA from classB. The
answer is yes. classA will not be accessible to users of classB.


Correction - if the function remains private in classA, then it doesn't
matter.
Jul 22 '05 #8
On Fri, 09 Jan 2004 07:42:31 -0500, Jeff Schwab wrote:
Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?

No.


I believe it does. Try this in the standard-compliant (ha) compiler of
your choice:

class A {
virtual void my_function() { }
}

class B: A {
void my_function() { A::myfunction( ); }
}

int main( )
{
A a;
}


Your point? You cannot call a member of a private base class, but how does
that relate to the issue at hand?

M4

Jul 22 '05 #9
Jeff Schwab wrote:
Rolf Magnus wrote:
s wrote:

class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?

Yes.


Right-o!
Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?

No.


I believe it does. Try this in the standard-compliant (ha) compiler
of your choice:

class A {
virtual void my_function() { }
}

class B: A {
void my_function() { A::myfunction( ); }


You call A::my_function (I assume you have a typo there), which is
private in A from a member function of B, which is of course not
possible, but that doesn't have anything to do with overriding the
virtual function. B can't call A::my_function, but A or a friend of A
could, and the polymorphism would still work.
}

int main( )
{
A a;
}

Jul 22 '05 #10
jeffc wrote:

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl...
On Thu, 08 Jan 2004 16:08:59 -0500, jeffc wrote:
>> Does the answer depend on how I inherit classA from classB (i.e.
>> public, protected, private)?
>
> Yes - you won't be able to access it externally if B uses private
> inheritance.
No, classA will not be accessable to users of classB, but that is
irrelevant for the question.


He asked if the answer depends on how he inherits classA from classB.


Right.
The answer is yes.
No, it isn't.
classA will not be accessible to users of classB.


Right, it won't. But that wasn't the question. classB does have a member
function my_function, and that function has lower access restrictions
than the one in A, and it does override A::my_function.

Jul 22 '05 #11
On Fri, 09 Jan 2004 10:36:55 -0500, jeffc wrote:

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl...
> On Thu, 08 Jan 2004 16:08:59 -0500, jeffc wrote:
>
> >> Does the answer depend on how I inherit classA from classB (i.e.

public,
> >> protected, private)?
> >
> > Yes - you won't be able to access it externally if B uses private
> > inheritance.
>
> No, classA will not be accessable to users of classB, but that is
> irrelevant for the question.


He asked if the answer depends on how he inherits classA from classB. The
answer is yes. classA will not be accessible to users of classB.


Correction - if the function remains private in classA, then it doesn't
matter.


It still doesn't matter anyhow, the writer of classB is allowed to
give the virtual any access he wants, it does not depend on the type of
inheritance.

M4

Jul 22 '05 #12

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl...

Correction - if the function remains private in classA, then it doesn't
matter.


It still doesn't matter anyhow, the writer of classB is allowed to
give the virtual any access he wants, it does not depend on the type of
inheritance.


What I was getting at (and I know it's irrelevant now) is the following:

class A
{
public:
void f();
};

class B : public A
{
};

B().f(); // this works

class B : private A
{
};

B().f(); // this does NOT work, so it matters if B publicly or privately
inherits from A
Jul 22 '05 #13
Martijn Lievaart wrote:
On Fri, 09 Jan 2004 07:42:31 -0500, Jeff Schwab wrote:

Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?
No.


I believe it does. Try this in the standard-compliant (ha) compiler of
your choice:

class A {
virtual void my_function() { }
}

class B: A {
void my_function() { A::myfunction( ); }
}

int main( )
{
A a;
}

Your point? You cannot call a member of a private base class, but how does
that relate to the issue at hand?


It doesn't. I saw "visibility" and thought "access."

Jul 22 '05 #14
Rolf Magnus wrote:
Jeff Schwab wrote:

Rolf Magnus wrote:
s wrote:

class A {
private:
virtual void my_function();
}

class B : public A {
private:
void my_function();
}

Am I allowed to increase the visibility of my_function in classB by
declaring it a public function?
Yes.


Right-o!

Does the answer depend on how I inherit classA from classB (i.e.
public, protected, private)?
No.


I believe it does. Try this in the standard-compliant (ha) compiler
of your choice:

class A {
virtual void my_function() { }
}

class B: A {
void my_function() { A::myfunction( ); }

You call A::my_function (I assume you have a typo there), which is
private in A from a member function of B, which is of course not
possible, but that doesn't have anything to do with overriding the
virtual function. B can't call A::my_function, but A or a friend of A
could, and the polymorphism would still work.


You are correct. As apology for the typo's, here's a version that
proves your point:

#include <iostream>

class A
{
virtual void my_function( ) { std::cout << "hello\n"; }
public:
void call_my_function( ) { my_function( ); }
};

class B: A
{
public:
virtual void my_function( ) { std::cout << "world\n"; }
};

int main( )
{
B b;

b.my_function( );
}

Jul 22 '05 #15

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

Similar topics

4
by: Kevin L | last post by:
Below is a code snippet that fails to compile under vc.net class BaseInterface { public: virtual void f() = 0; }; class BaseImplementation : public BaseInterface {
8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
22
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...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
7
by: shintu | last post by:
Hi, For the following code snippet, the compiler complains for "mi *mi1=new mi;" statement //******************************************************* #include <iostream> using namespace std;...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
2
by: mmcgarry.work | last post by:
Hi, I would like to follow Stroustrup's advice of separating an object interface (abstract class) from an object implementation (concrete class), See Section 15.2.5 in Stroustrup 3rd Edition. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.