Inheritance and virtual functions

R

Rolf Magnus

s said:
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.
 
S

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)?
 
J

jeffc

s said:
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.
 
M

Martijn Lievaart

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
 
P

Peter Koch Larsen

s said:
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
 
J

Jeff Schwab

Rolf said:
s wrote:



Right-o!



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;
}
 
J

jeffc

Martijn Lievaart said:
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.
 
J

jeffc

jeffc said:
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.
 
M

Martijn Lievaart

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
 
R

Rolf Magnus

Jeff said:
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.
 
R

Rolf Magnus

jeffc said:
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.
 
M

Martijn Lievaart

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
 
J

jeffc

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
 
J

Jeff Schwab

Martijn said:
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."
 
J

Jeff Schwab

Rolf said:
Jeff Schwab wrote:




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( );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top