Changing access specifier for virtual function

D

dragoncoder

Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?

Thanks
 
T

Thomas Tutone

dragoncoder said:
Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?

Yes. And an excellent example of poor coding practice.
2. If the virtual function is private in Base, can I make it public in
Derived ?

No. If it were private in Base, then Derived wouldn't inherit it.

Best regards,

Tom
 
V

Victor Bazarov

dragoncoder said:
Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {

You mean

class Derived: public Base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?

Yes, absolutely, considering the correction I made. Try not to type your
code directly into the message next time. Use "copy-and-paste" mechanism
undoubtedly available to you.
2. If the virtual function is private in Base, can I make it public in
Derived ?

The access specifier only has effect on the ability to call the function
as if it weren't virtual. For example, if you make 'say' private in 'Base',
your call to it in 'main' (b->say()) will be ill-formed, and won't compile.

V
 
V

Victor Bazarov

Thomas said:
Yes. And an excellent example of poor coding practice.

Why do you call this practice "poor"?
No. If it were private in Base, then Derived wouldn't inherit it.

Nothing is inherited here. It's _overridden_. Try it:

#include <iostream>

class Base {
virtual int foo() { return 42; } // private
public:
int bar() { return foo(); }
};

class Derived : public Base {
virtual int foo() { return 73; } // also private
};

int main()
{
Derived d;
Base *b = &d;
std::cout << b->bar() << std::endl;
return 0;
}

What happens here?

V
 
T

Tejas Kokje

dragoncoder said:
Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?

Since the above code compiled, it is syntactically legal.
2. If the virtual function is private in Base, can I make it public in
Derived ?

The call to b->say() is calling Derived class say() function. Here
Derived class does not know anything about say() function of Base class
since say() is private in Base(). Hence say() functions in Derived and
Base class are not related by virtual function paradigm.

Even if you remove "virtual" specifier from Base class say() function,
you will still run Derived class say() function since say() is public
in Derived and there is no public say() in Base class.

Tejas Kokje
 
P

Phlip

Tejas said:
Since the above code compiled, it is syntactically legal.

Famous last words.

Try: "The code is well-formed, so it should compile."

Plenty of compilers reject plenty of well-formed constructions.

Yes, the OP is correct to rely on their compiler to judge how well such a
simple bit of code is formed!
 
T

Tejas Kokje

Phlip said:
Famous last words.

Try: "The code is well-formed, so it should compile."
Plenty of compilers reject plenty of well-formed constructions.
Yes, the OP is correct to rely on their compiler to judge how well such a
simple bit of code is formed!

I think you misunderstood my comments. I said that the code is
*syntactically* legal. This does not mean that code semantics make
sense. If compilers could detect semantic errors in code, most of the
softwares would be bug free.

Anyways, in OP's case, compiler did compile the code. So there is no
reason for him/her to see the code as illegal. However, OP is not
relying on compiler to tell him that code is well formed. Hence he/she
posted a question here.

Tejas Kokje
 
J

Jakob Bieling

Tejas Kokje said:
dragoncoder wrote:
Since the above code compiled, it is syntactically legal.

No. Most compilers are broken in one respect or the other. I do not
know if Comeau is 100% compliant even. The code below will compile with
some compilers, but it is *not* syntactically legal:

class foo
{
void bar () {}
};

int main ()
{
void (foo::*pbar) () = foo::bar;
}
regards
 
T

Tejas Kokje

Can you tell me which compiler compiles above code ?

since bar() is private and non-static to class foo, how can you access
it outside the class using class scope ?

Tejas Kokje
 
D

Daniel T.

"dragoncoder said:
Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?

Absolutely. It's a great way to make sure the derived object is used
through a base pointer.

2. If the virtual function is private in Base, can I make it public in
Derived ?

Yes.
 
D

Daniel T.

"Tejas Kokje said:
Can you tell me which compiler compiles above code ?

since bar() is private and non-static to class foo, how can you access
it outside the class using class scope ?

One of the compilers I use will in fact compile the code despite the
fact that bar() is private and non-static to class foo.

It's used in NintendoDS development. It also can't handle exceptions or
the STL.
 
T

Thomas Tutone

Victor said:
Thomas Tutone wrote:
dragoncoder wrote:
[snip]
Yes. And an excellent example of poor coding practice.

Why do you call this practice "poor"?

Because it violates substitutability. Derived supposedly "is-a" Base,
and yet say() is no longer a public member function. See, for example,
[Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it proper to
revoke (hide) an inherited public member function?")].
Nothing is inherited here. It's _overridden_.

Yes, you're right - I confused my terminology. Thank you for the
correction.

Best regards,

Tom
 
V

Victor Bazarov

Thomas said:
Victor said:
Thomas Tutone wrote:
dragoncoder wrote:
[snip]
class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};
I have 2 questions:
1. Is the above code legal ?

Yes. And an excellent example of poor coding practice.

Why do you call this practice "poor"?

Because it violates substitutability. Derived supposedly "is-a" Base,
and yet say() is no longer a public member function. See, for
example, [Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it
proper to revoke (hide) an inherited public member function?")].

I don't have that book, if you care to discuss issues brough up in it,
quote it, and we will know what you're talking about.

If the client (user) code only knows about the Base class (and only cares
about the Base class), changing the derived class' virtual members' access
specifiers (a) does not break the polymorphic behaviour and (b) prevents
accidental non-polymorphic use of those functions where a call to Base is
intended. The compiler does not make decisions to resolve the call based
on the access specifiers, but if it instead of intended use of Base member
attempts to use Derived's member, the privateness of the Derived's member
will cause the compiler to barf.

Besides, an even better practice would actually be to hide _both_ 'say'
members in the private area, define another, non-virtual function in Base
and make it call 'say':

class Base {
virtual void say();
public:
void talk() { say{}; }
};

In this case nobody should complain about changing the access levels of
the interface components.

V
 
D

Daniel T.

"Thomas Tutone said:
Victor said:
Thomas Tutone wrote:
dragoncoder wrote:
[snip]
class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};
I have 2 questions:
1. Is the above code legal ?

Yes. And an excellent example of poor coding practice.

Why do you call this practice "poor"?

Because it violates substitutability. Derived supposedly "is-a" Base,
and yet say() is no longer a public member function. See, for example,
[Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it proper to
revoke (hide) an inherited public member function?")].

also at
<http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.1>
Attempting to hide (eliminate, revoke, privatize) inherited public
member functions is an all-too-common design error. It usually stems
from muddy thinking.

In the above code, Derived does not eliminate, revoke or privatize
anything. Any user of Derived can still call the 'say' function (by
using a Base*.)

Unfortunately, Cline doesn't give an example of what he means (either on
the website or in the book.)
 

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,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top