473,396 Members | 1,963 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,396 software developers and data experts.

Changing access specifier for virtual function

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

Apr 7 '06 #1
13 2806
dragoncoder wrote:
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

Apr 7 '06 #2
dragoncoder wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 7 '06 #3
Thomas Tutone wrote:
dragoncoder wrote:
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.


Why do you call this practice "poor"?
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.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 7 '06 #4
dragoncoder wrote:
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

Apr 7 '06 #5
Tejas Kokje wrote:
1. Is the above code legal ?


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!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 7 '06 #6
Phlip wrote:
Tejas Kokje wrote:
1. Is the above code legal ?
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!


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

Apr 7 '06 #7
Tejas Kokje <bi*************@gmail.com> wrote:
dragoncoder wrote:
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.


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
--
jb

(reply address in rot13, unscramble first)
Apr 7 '06 #8
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

Apr 7 '06 #9
In article <11**********************@g10g2000cwb.googlegroups .com>,
"dragoncoder" <pk******@gmail.com> wrote:
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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 7 '06 #10
In article <11*********************@i40g2000cwc.googlegroups. com>,
"Tejas Kokje" <bi*************@gmail.com> wrote:
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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 7 '06 #11
Victor Bazarov wrote:
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?")].
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.


Nothing is inherited here. It's _overridden_.


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

Best regards,

Tom

Apr 8 '06 #12
Thomas Tutone wrote:
Victor Bazarov wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 8 '06 #13
In article <11**********************@j33g2000cwa.googlegroups .com>,
"Thomas Tutone" <Th***********@yahoo.com> wrote:
Victor Bazarov wrote:
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.)
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 9 '06 #14

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

Similar topics

3
by: mshetty | last post by:
Hi, I have a class x with two public methods xmethod1 and xmethod2. I want to derive a class y such that it can access only xmethod2. I have done the following and it compiles please let me know...
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
1
by: Paul Cheetham | last post by:
Hi, I am creating a custom control in c#, derived from System.Drawing.Image In my derived control, I want to make the Public property Image into a Protected property, as I only want to...
3
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
2
by: mrclash | last post by:
Hello, I have a class that uses some variables to access a sql database, and I want to make a derived class that inherits all the methods of this class, because the derived one will do exactly...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
16
by: Wade Ward | last post by:
/* C version */ static unsigned long t,x=123456789,y=362436069,z=21288629,w=14921776,c=0; unsigned long KISS(){ x+=545925293; y^=(y<<13); y^=(y>>17); y^=(y<<5); t=z+w+c; z=w; c=(t>>31);...
3
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.